Unified · Split
| @@ -72,6 +72,8 @@ sk_core * sk_core_new(int sr) · 71 unmodified lines | ||
| 72 | 72 | |
| 73 | 73 | sk_stack_init(&core->stack); |
| 74 | 74 | sk_regtbl_init(&core->regtbl); |
| 75 | + | |
| 76 | + sk_core_srand(core, 0); | |
| 75 | 77 | return core; |
| 76 | 78 | } |
| 77 | 79 | #+END_SRC |
| @@ -102,6 +104,7 @@ struct sk_core { · 24 unmodified lines | ||
| 102 | 104 | pw_patch *patch; |
| 103 | 105 | sk_stack stack; |
| 104 | 106 | sk_regtbl regtbl; |
| 107 | + unsigned long rng; | |
| 105 | 108 | }; |
| 106 | 109 | #+END_SRC |
| 107 | 110 | |
| @@ -1295,3 +1298,45 @@ check an error code and exit if it is non-zero. · 1187 unmodified lines | ||
| 1295 | 1298 | return 1;\ |
| 1296 | 1299 | } |
| 1297 | 1300 | #+END_SRC |
| 1301 | +* Random Number Generator | |
| 1302 | +A simple random number generator (RNG) | |
| 1303 | +is included with the core API, based | |
| 1304 | +on the LCG found in Soundpipe. | |
| 1305 | + | |
| 1306 | +Seed the RNG with =sk_core_srand=. | |
| 1307 | + | |
| 1308 | +#+NAME: funcdefs | |
| 1309 | +#+BEGIN_SRC c | |
| 1310 | +void sk_core_srand(sk_core *core, unsigned long val); | |
| 1311 | +#+END_SRC | |
| 1312 | + | |
| 1313 | +#+NAME: funcs | |
| 1314 | +#+BEGIN_SRC c | |
| 1315 | +void sk_core_srand(sk_core *core, unsigned long val) | |
| 1316 | +{ | |
| 1317 | + core->rng = val; | |
| 1318 | +} | |
| 1319 | +#+END_SRC | |
| 1320 | + | |
| 1321 | +A random number is generated with =sk_core_rand=. | |
| 1322 | + | |
| 1323 | +#+NAME: funcdefs | |
| 1324 | +#+BEGIN_SRC c | |
| 1325 | +unsigned long sk_core_rand(sk_core *core); | |
| 1326 | +#+END_SRC | |
| 1327 | + | |
| 1328 | +This will return a value between 0 and =SK_CORE_RANDMAX=. | |
| 1329 | + | |
| 1330 | +#+NAME: macros | |
| 1331 | +#+BEGIN_SRC c | |
| 1332 | +#define SK_CORE_RANDMAX 2147483648 | |
| 1333 | +#+END_SRC | |
| 1334 | + | |
| 1335 | +#+NAME: funcs | |
| 1336 | +#+BEGIN_SRC c | |
| 1337 | +unsigned long sk_core_rand(sk_core *core) | |
| 1338 | +{ | |
| 1339 | + core->rng = (1103515245 * core->rng + 12345) % SK_CORE_RANDMAX; | |
| 1340 | + return core->rng; | |
| 1341 | +} | |
| 1342 | +#+END_SRC | |