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
7272
7373 sk_stack_init(&core->stack);
7474 sk_regtbl_init(&core->regtbl);
75+
76+ sk_core_srand(core, 0);
7577 return core;
7678 }
7779 #+END_SRC
@@ -102,6 +104,7 @@ struct sk_core { · 24 unmodified lines
102104 pw_patch *patch;
103105 sk_stack stack;
104106 sk_regtbl regtbl;
107+ unsigned long rng;
105108 };
106109 #+END_SRC
107110
@@ -1295,3 +1298,45 @@ check an error code and exit if it is non-zero. · 1187 unmodified lines
12951298 return 1;\
12961299 }
12971300 #+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

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