source: azure_iot_hub/trunk/musl-1.1.18/src/prng/random.c@ 388

Last change on this file since 388 was 388, checked in by coas-nagasima, 5 years ago

Azure IoT Hub Device C SDK を使ったサンプルの追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 2.1 KB
Line 
1#include <stdlib.h>
2#include <stdint.h>
3#include "libc.h"
4
5/*
6this code uses the same lagged fibonacci generator as the
7original bsd random implementation except for the seeding
8which was broken in the original
9*/
10
11static uint32_t init[] = {
120x00000000,0x5851f42d,0xc0b18ccf,0xcbb5f646,
130xc7033129,0x30705b04,0x20fd5db4,0x9a8b7f78,
140x502959d8,0xab894868,0x6c0356a7,0x88cdb7ff,
150xb477d43f,0x70a3a52b,0xa8e4baf1,0xfd8341fc,
160x8ae16fd9,0x742d2f7a,0x0d1f0796,0x76035e09,
170x40f7702c,0x6fa72ca5,0xaaa84157,0x58a0df74,
180xc74a0364,0xae533cc4,0x04185faf,0x6de3b115,
190x0cab8628,0xf043bfa4,0x398150e9,0x37521657};
20
21static int n = 31;
22static int i = 3;
23static int j = 0;
24static uint32_t *x = init+1;
25static volatile int lock[2];
26
27static uint32_t lcg31(uint32_t x) {
28 return (1103515245*x + 12345) & 0x7fffffff;
29}
30
31static uint64_t lcg64(uint64_t x) {
32 return 6364136223846793005ull*x + 1;
33}
34
35static void *savestate() {
36 x[-1] = (n<<16)|(i<<8)|j;
37 return x-1;
38}
39
40static void loadstate(uint32_t *state) {
41 x = state+1;
42 n = x[-1]>>16;
43 i = (x[-1]>>8)&0xff;
44 j = x[-1]&0xff;
45}
46
47static void __srandom(unsigned seed) {
48 int k;
49 uint64_t s = seed;
50
51 if (n == 0) {
52 x[0] = s;
53 return;
54 }
55 i = n == 31 || n == 7 ? 3 : 1;
56 j = 0;
57 for (k = 0; k < n; k++) {
58 s = lcg64(s);
59 x[k] = s>>32;
60 }
61 /* make sure x contains at least one odd number */
62 x[0] |= 1;
63}
64
65void srandom(unsigned seed) {
66 LOCK(lock);
67 __srandom(seed);
68 UNLOCK(lock);
69}
70
71char *initstate(unsigned seed, char *state, size_t size) {
72 void *old;
73
74 if (size < 8)
75 return 0;
76 LOCK(lock);
77 old = savestate();
78 if (size < 32)
79 n = 0;
80 else if (size < 64)
81 n = 7;
82 else if (size < 128)
83 n = 15;
84 else if (size < 256)
85 n = 31;
86 else
87 n = 63;
88 x = (uint32_t*)state + 1;
89 __srandom(seed);
90 savestate();
91 UNLOCK(lock);
92 return old;
93}
94
95char *setstate(char *state) {
96 void *old;
97
98 LOCK(lock);
99 old = savestate();
100 loadstate((uint32_t*)state);
101 UNLOCK(lock);
102 return old;
103}
104
105long random(void) {
106 long k;
107
108 LOCK(lock);
109 if (n == 0) {
110 k = x[0] = lcg31(x[0]);
111 goto end;
112 }
113 x[i] += x[j];
114 k = x[i]>>1;
115 if (++i == n)
116 i = 0;
117 if (++j == n)
118 j = 0;
119end:
120 UNLOCK(lock);
121 return k;
122}
Note: See TracBrowser for help on using the repository browser.