History

added RNG to core

Paul Batchelor committed 5 years agomaster

Commit f48c7a91 parent c4ef1b61 file · +45 −0Browse files

Unified · Split

core.org+45−0
@@ -72,6 +72,8 @@ sk_core * sk_core_new(int sr) · 71 unmodified lines
72 72
73 sk_stack_init(&core->stack);73 sk_stack_init(&core->stack);
74 sk_regtbl_init(&core->regtbl);74 sk_regtbl_init(&core->regtbl);
75
76 sk_core_srand(core, 0);
75 return core;77 return core;
76}78}
77#+END_SRC79#+END_SRC
@@ -102,6 +104,7 @@ struct sk_core { · 24 unmodified lines
102 pw_patch *patch;104 pw_patch *patch;
103 sk_stack stack;105 sk_stack stack;
104 sk_regtbl regtbl;106 sk_regtbl regtbl;
107 unsigned long rng;
105};108};
106#+END_SRC109#+END_SRC
107 110
@@ -1295,3 +1298,45 @@ check an error code and exit if it is non-zero. · 1187 unmodified lines
1295 return 1;\1298 return 1;\
1296}1299}
1297#+END_SRC1300#+END_SRC
1301* Random Number Generator
1302A simple random number generator (RNG)
1303is included with the core API, based
1304on the LCG found in Soundpipe.
1305
1306Seed the RNG with =sk_core_srand=.
1307
1308#+NAME: funcdefs
1309#+BEGIN_SRC c
1310void sk_core_srand(sk_core *core, unsigned long val);
1311#+END_SRC
1312
1313#+NAME: funcs
1314#+BEGIN_SRC c
1315void sk_core_srand(sk_core *core, unsigned long val)
1316{
1317 core->rng = val;
1318}
1319#+END_SRC
1320
1321A random number is generated with =sk_core_rand=.
1322
1323#+NAME: funcdefs
1324#+BEGIN_SRC c
1325unsigned long sk_core_rand(sk_core *core);
1326#+END_SRC
1327
1328This 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
1337unsigned 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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t