User loginNavigation 
Functional random numbers without threading stateI was thinking about this problem the other day and came up with what I think is a tenable solution. It's quite possible someone else has thought of this before but I haven't seen it. The idea is very simple: provide the programmer with a function random(x), which deterministically maps real numbers to random values (e.g. a white noise function). Within any function definition (or other code unit), the programmer obtains random numbers by calling the function with different arguments, e.g.: let my_random_data = [random(0), random(1), random(2)] Because random(x) is deterministic, these expressions can be reused at will, returning their original results: let my_random_data = [random(0), random(1), random(2), random(0), random(1), random(2)] Now, to enable modular use of this function, we can simply derive from it a new function, random'(y), whose domain is again the real numbers, but maps to an interval in the domain of random(x), say 0<x<1: let random'(y) = random(atan(y) / pi + 0.5) Now another function or module can use random'(y) in the same manner as the main code used random(x), without fear of duplicating the random sequence. A new derived function can be created for each module which requires its own random function, simply by choosing a different interval of random(x) to map into: let random''(z) = random(atan(z) / pi + 1.5) This process can be repeated within submodules, etc. Of course, you can simplify this pattern using a higherorder function, which I will call "delve": let delve(random, index) = (x) > random(atan(x) / pi + index + 0.5) So an example program might look like: let create_random_data(random') = [random'(0), random'(1), random'(2)] let my_random_data = > [0.53, 0.12, 0.79, 0.16, 0.08, 0.92, 0.65, 0.50, 0.01] I wrote an OCaml module implementing roughly this interface, albeit over integers, which is available here (interface). It reuses the builtin sequential random number generator, and is optimized for the case when the random function is accessed sequentially. (It must "replay" the random sequence if it is accessed otherwise.) The "delve" function is implemented by using the value from the given random sequence as a seed for a new random sequence. Thoughts? Improvements? By Chris King at 20101008 14:18  LtU Forum  previous forum topic  next forum topic  other blogs  3677 reads

Browse archivesActive forum topicsNew forum topics 
Recent comments
1 day 20 hours ago
2 days 19 hours ago
3 days 13 hours ago
3 days 22 hours ago
5 days 18 hours ago
6 days 13 min ago
6 days 10 hours ago
6 days 11 hours ago
1 week 2 days ago
1 week 2 days ago