-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.cs
executable file
·83 lines (63 loc) · 2.27 KB
/
generator.cs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh
#-*-mode:c-*-
(echo "#line 3 \"$0\"";echo;tail -n +4 $0) >/tmp/cs.$$.c && gcc -Wall -o /tmp/cs.$$ /tmp/cs.$$.c && /tmp/cs.$$ $*;rm -f /tmp/cs.$$*; exit
/*
Usage: ./generator.cs QUANTITY MAGNITUDE REDUNDANCY
Generates an arbitraty QUANTITY of "random" numbers. Each number is
scaled down to a certain binary MAGNITUDE (e.g. if you set this
parameter to 10, then all your numbers will be smaller than 1024).
The script also include a REDUNDANCY parameter to artificially control
the "variety" of the results. Setting this value to 100 will cause all
numbers to be the same. Setting this value to 0 will cause all numbers
to be really random (even though we make no attempt to eliminate
fortuitous repetitions)
You should redirect the ouput in to a file, like in the examples below:
./generator.cs 50 32 0 > fifty_distinct_smallish_numbers.txt
./generator.cs 10 64 100 > one_large_number_repeated_ten_times.txt
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
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 = 20;
if( argc > 1)
quantity=atoi(argv[1]);
// maximum magnitude of numbers, in bits (0..64)
int magnitude= 64;
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=50;
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("%ju\n",(intmax_t)number);
}
return 0;
}