blob: 9729734356c0b59d3feda77dc8dd00c52a04e80c (
plain) (
blame)
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
|
Use the modern random(3) and device-based seeding.
-mi
--- src/rand.c 2009-11-18 07:23:18.000000000 -0500
+++ src/rand.c 2013-02-12 16:09:01.000000000 -0500
@@ -1,26 +1,18 @@
#include "super.h"
-int myrand(void)
-{
- struct timeval tv;
- unsigned int seed;
-
- gettimeofday(&tv, (struct timezone *) 0);
- seed = (tv.tv_sec % 10000) * 523 + tv.tv_usec * 13 + (getpid() % 1000) * 983;
- srand(seed);
-
- return rand();
-}
-
-char *rand2str(size_t len)
+const char *
+rand2str(size_t len)
{
static char buf[BUFSIZ];
char *ptr = buf;
- char *alphabet =
+ char alphabet[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- int i;
+ size_t i;
+
+ if (len == 0)
+ return NULL;
- if (len < 0)
- return (char *) 0;
+ if (buf[0] == '\0') /* First time we are here */
+ srandomdev(); /* Seed */
if (len >= BUFSIZ)
@@ -28,5 +20,5 @@
for (i = 0; i < len; i++) {
- int j = (myrand() & 0xffff) % (26 + 26 + 10);
+ int j = random() % (sizeof(alphabet)/sizeof(char) - 1);
ptr[i] = alphabet[j];
}
|