Provides functions to create sequences of 160-bit "random" numbers, starting from a 160-bit seed number.
This program implements a "Pseudo-Random Number Generator". All generated numbers are 160 bits long; it also allows for having more than one "sequence" going on at the same time (eg. with different seeds).
We have only two functions, make-sequence which initializes a sequence object given a seed, and next-number that returns the next number in the sequence given a sequence object.
〈Overview〉 ≡
make-sequence: func [
"Create a random number sequence"
seed [binary!]
] [
〈Initialize a sequence object〉
]
next-number: func [
"Return the next number from the sequence"
sequence [object!]
] [
〈Generate the next number in the sequence〉
]
The implementation is trivial and is based on keyed SHA-1 (HMAC).
〈Initialize a sequence object〉 ≡
context [
cur: #{0000000000000000000000000000000000000000}
key: seed
]
〈Generate the next number in the sequence〉 ≡
sequence/cur: checksum/secure/key sequence/cur sequence/key