-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbr_generator.c
62 lines (51 loc) · 1.55 KB
/
nbr_generator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define NBR_QUANTITY 100
#define NBR_MAGNITUDE 64
#define NBR_REDUNDANCY 40
int main(int argc, char *argv[])
{
uint64_t number ;
uint32_t * word = (void*) & number ;
uint64_t *previous_numbers;
// how many numbers to generate
int quantity = NBR_QUANTITY; //default value
scanf("%d",&quantity);
if( argc > 1)
quantity=atoi(argv[1]);
// maximum magnitude of numbers, in bits (0..64)
int magnitude= NBR_MAGNITUDE; //default value
scanf("%d",&magnitude);
if( argc > 2)
magnitude=atoi(argv[2]);
// percentage of redundancy (0..100)
// 30% means each number only has 2/3 chance to be a brand new one
int redundancy=NBR_REDUNDANCY;
if( argc > 3)
redundancy=atoi(argv[3]);
// we seed the the generator with a constant value so as to get
// reproducible results.
srandom(0);
previous_numbers=malloc(quantity*sizeof(uint64_t));
int i;
for(i=0; i<quantity; i++)
{
if( i==0 || random() % 100 > redundancy)
{
// let's generate a new number
word[0] = random();
word[1] = random();
// shift right to reduce magnitude
number >>= 64-magnitude ;
}
else
{
// let's pick from previously generated numbers
number = previous_numbers[ random() % i ];
}
previous_numbers[i] = number;
printf("%llu\n",(long long)number);
}
return 0;
}