4 ms·
Can you give an example usage? I cannot figure out how to invoke your PRNG.
by queuebert 4y ago
Can you give an example usage? I cannot figure out how to invoke your PRNG.
- gavinhoward 4y agoIf you want just a number to store in a hardware integer type, use this: echo "rand()" | bc If you want a bounded integer, use this: echo "irand(<bound>)" | bc This bound is adjusted to not bias the random numbers. It can also be used to generate either really small or really large integers, arbitrary size. My bc will take care of everything behind the scenes. If you want to generate a real between 0 and 1, use this: echo "frand(<places>)" | bc -l where <places> is the number of decimal places you want. Note the "-l" argument; this function is not built-in like the other two. If you want an arbitrary real (not between 0 and 1), use this: echo "ifrand(<bound>, <places>)" | bc -l My bc will seed itself from the system CSPRNG, but you can seed the PRNG by assigning the seed to the special variable `seed` first: echo "seed = $SEED; rand()" | bc You can even save the current seed by getting the value of the `seed` variable: echo "seed" | bc If you want to use the same sequence over multiple invocations of bc, you need to save the seed to pass to the next invocation: OUT=$(echo "rand(); seed" | bc) RAND=$(echo "$OUT" | head -n1) SEED=$(echo "$OUT" | tail -n1) RAND2=$(echo "seed = $SEED; rand()" | bc) (Haven't tried the above code. Sorry if it's wrong.) This pattern works because the `seed` variable is updated after every call to the PRNG. Does that help?
- queuebert 4y agoThank you, this is perfect!