Browse Source

Split random number generation out into core/random.c, and create the

correct prototypes for srandom(), rand() and srand().
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
4256b3338a
3 changed files with 82 additions and 22 deletions
  1. 0
    18
      src/core/misc.c
  2. 38
    0
      src/core/random.c
  3. 44
    4
      src/include/stdlib.h

+ 0
- 18
src/core/misc.c View File

55
 	return (~checksum) & 0xFFFF;
55
 	return (~checksum) & 0xFFFF;
56
 }
56
 }
57
 
57
 
58
-
59
-
60
-/**************************************************************************
61
-RANDOM - compute a random number between 0 and 2147483647L or 2147483562?
62
-**************************************************************************/
63
-long int random(void)
64
-{
65
-	static int32_t seed = 0;
66
-	int32_t q;
67
-	if (!seed) /* Initialize linear congruential generator */
68
-		seed = currticks();
69
-	/* simplified version of the LCG given in Bruce Schneier's
70
-	   "Applied Cryptography" */
71
-	q = seed/53668;
72
-	if ((seed = 40014*(seed-53668*q) - 12211*q) < 0) seed += 2147483563L;
73
-	return seed;
74
-}
75
-
76
 /**************************************************************************
58
 /**************************************************************************
77
 SLEEP
59
 SLEEP
78
 **************************************************************************/
60
 **************************************************************************/

+ 38
- 0
src/core/random.c View File

1
+/** @file
2
+ *
3
+ * Random number generation
4
+ *
5
+ */
6
+
7
+#include <stdlib.h>
8
+
9
+static int32_t rnd_seed = 0;
10
+
11
+/**
12
+ * Seed the pseudo-random number generator
13
+ *
14
+ * @v seed		Seed value
15
+ */
16
+void srandom ( unsigned int seed ) {
17
+	rnd_seed = seed;
18
+}
19
+
20
+/**
21
+ * Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
22
+ *
23
+ * @ret rand		Pseudo-random number
24
+ */
25
+long int random ( void ) {
26
+	int32_t q;
27
+
28
+	if ( ! rnd_seed ) /* Initialize linear congruential generator */
29
+		srandom ( currticks() );
30
+
31
+	/* simplified version of the LCG given in Bruce Schneier's
32
+	   "Applied Cryptography" */
33
+	q = ( rnd_seed / 53668 );
34
+	rnd_seed = ( 40014 * ( rnd_seed - 53668 * q ) - 12211 * q );
35
+	if ( rnd_seed < 0 )
36
+		rnd_seed += 2147483563L;
37
+	return rnd_seed;
38
+}

+ 44
- 4
src/include/stdlib.h View File

2
 #define STDLIB_H
2
 #define STDLIB_H
3
 
3
 
4
 #include <stdint.h>
4
 #include <stdint.h>
5
+#include <assert.h>
6
+
7
+/*****************************************************************************
8
+ *
9
+ * Numeric parsing
10
+ *
11
+ ****************************************************************************
12
+ */
5
 
13
 
6
 extern unsigned long strtoul ( const char *p, char **endp, int base );
14
 extern unsigned long strtoul ( const char *p, char **endp, int base );
7
-extern void * realloc ( void *old_ptr, size_t new_size );
15
+
16
+/*****************************************************************************
17
+ *
18
+ * Memory allocation
19
+ *
20
+ ****************************************************************************
21
+ */
22
+
8
 extern void * malloc ( size_t size );
23
 extern void * malloc ( size_t size );
24
+extern void * realloc ( void *old_ptr, size_t new_size );
9
 extern void free ( void *ptr );
25
 extern void free ( void *ptr );
10
-extern int system ( const char *command );
11
-extern long int random ( void );
12
-
13
 extern void * _calloc ( size_t len );
26
 extern void * _calloc ( size_t len );
14
 
27
 
15
 /**
28
 /**
29
 	return _calloc ( nmemb * size );
42
 	return _calloc ( nmemb * size );
30
 }
43
 }
31
 
44
 
45
+/*****************************************************************************
46
+ *
47
+ * Random number generation
48
+ *
49
+ ****************************************************************************
50
+ */
51
+
52
+extern long int random ( void );
53
+extern void srandom ( unsigned int seed );
54
+
55
+static inline int rand ( void ) {
56
+	return random();
57
+}
58
+
59
+static inline void srand ( unsigned int seed ) {
60
+	srandom ( seed );
61
+}
62
+
63
+/*****************************************************************************
64
+ *
65
+ * Miscellaneous
66
+ *
67
+ ****************************************************************************
68
+ */
69
+
70
+extern int system ( const char *command );
71
+
32
 #endif /* STDLIB_H */
72
 #endif /* STDLIB_H */

Loading…
Cancel
Save