-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrand_utils.c
More file actions
59 lines (50 loc) · 1.62 KB
/
rand_utils.c
File metadata and controls
59 lines (50 loc) · 1.62 KB
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
#include <rand_utils.h>
#include <stdlib.h> // For random(), RAND_MAX
#include <sys/time.h> // For gettimeofday, part of POSIX
#include <stdio.h>
#include <math.h>
// rand() and srand() are part of the standard library of C
// random() and srandom() are part of POSIX and not available on Windows
/* Numerical Recipes book: "be very, very suspicious of a system-supplied rand(); System-supplied rand()s are almost always linear congruential generators */
// In GNU library, RAND_MAX is 2147483647 but portable code cannot assume more than 32767
// C or C++ offer random(), which returns a long integer in the range 0 to RAND_MAX, so ((double)random())/RAND_MAX is a double that lies in the range 0.0 to 1.0.
// Generates an integer between [min,max)
DTYPE
rand_int(int min, int max) {
// http://www.eternallyconfuzzled.com/arts/jsw_art_rand.aspx
// math implementation: http://stackoverflow.com/questions/2509679/how-to-generate-a-random-number-from-within-a-range
double r;
r = rand_double();
//printf("WHAT DO YOU THINK????\n");
int range = max-min;
// printf("%lf\n", r*range);
return (DTYPE) (r*range) + min;
}
int
rand_binary() {
double r = rand_double();
return round(r);
}
// Ensures values between 0 and 1
double
rand_double() {
// printf("random()=%lf\n", (double)random());
// printf("Random is: %f\n", (double)random()/(RAND_MAX+1.0));
return (double)random()/(RAND_MAX+1.0);
}
double
rand_double_full() {
return (double)random();
}
void
set_seed(int seed) {
srandom(seed);
}
int
set_random_seed() {
struct timeval tv;
gettimeofday(&tv, NULL);
int seed = (int) (tv.tv_sec + tv.tv_usec);
srandom(seed);
return seed;
}