Is there a way to obtain the state of the random number generator? [duplicate] - random

This question already has answers here:
Retrieve RNG seed in julia
(6 answers)
Closed 5 years ago.
Say I seed 123 with srand(123), and run rand() X times. Later, I want to be able to restart Julia and seed a number (or state) such that when I run rand() again I get the number that would have been generated if I had seed 123 and run rand() X + 1 times. Is there any way I can do that, or do I really have to run rand() X times to obtain the state I want?

If the solution with custom random number generator presented in Retrieve RNG seed in julia is not feasible for you the best I can come up with is to copy the whole structure of global random number generator:
function reset_global_rng(rng_state)
Base.Random.GLOBAL_RNG.seed = rng_state.seed
Base.Random.GLOBAL_RNG.state = rng_state.state
Base.Random.GLOBAL_RNG.vals = rng_state.vals
Base.Random.GLOBAL_RNG.idx = rng_state.idx
end
rs = deepcopy(Base.Random.GLOBAL_RNG)
println(rand(5))
# [0.301558,0.602108,0.220952,0.0338732,0.553414]
reset_global_rng(rs)
println(rand(5))
# [0.301558,0.602108,0.220952,0.0338732,0.553414]
although I am not 100% sure how it does not come into interaction with dsfmt_gv_srand() in random.jl.

Related

Question Regarding Random Number Generation in Lua

I was playing around with the lua interpretor and made this little program that generates two numbers and compares them. The program runs until the numbers match. The first number is randomly generated using math.random(), and is set to 1, and 100000. The second value that is generated to compare is between 1 and 100. It also keeps track of how many times the program loops. The program works as intended, but something strange happens when I run it.
The values that come up are always either 1, 31, 62, or 92. I've run the program many times, but it keeps generating these numbers. I have some understanding of how random numbers are generated, but this just seems weird. I'll paste the code in below. If someone can explain what's going on here, I would appreciate it greatly. Thanks!
counter=0;
a=0;
b=1;
while(a~=b)do
a=math.random(1,1000000);
b=math.random(1,100);
counter=counter+1;
if(a==b)then
print(a..", "..b..", and it took "..counter.." times")
end
end
The behaviour of the pseudo-random number generator changed in Lua 5.4. The 5.4 Reference Manual states, under §8.2 – Incompatibilities in the Libraries, that
The pseudo-random number generator used by the function math.random now starts with a somewhat random seed. Moreover, it uses a different algorithm.
If you are curious, that new algorithm is xoshiro256**.
What this means is that programs running in Lua environments prior to 5.4 must explicitly call math.randomseed to seed the pRNG. You want to do this only once in your program.
Without seeding the pRNG, the sequence of numbers produced will be the same each time you run your program. This is because, prior to 5.4, Lua's math.random is implemented using C rand or POSIX random, both of which default to a seed of 1 if not explicitly seeded.
The classic way to seed a pRNG is to use the current time (os.time). This is a simple approach, but has the fault that running this program twice in the same second will have the same result.
local counter = 0
local a = 0
local b = 1
math.randomseed(os.time())
while a ~= b do
a = math.random(1, 1000000)
b = math.random(1, 100)
counter = counter + 1
if a == b then
print(a .. ", " .. b .. ", and it took " .. counter .. " times")
end
end
Documentation links, for comparison:
5.3: math.random math.randomseed
5.4: math.random math.randomseed

How can I generate a random number between 0-5 in Swift? [duplicate]

This question already has an answer here:
How to generate a random number in a range (10...20) using Swift [duplicate]
(1 answer)
Closed 6 years ago.
I have only been coding in my off time for a couple of weeks now and am creating an app with a very basic "guess how many" concept. The user will enter a number between 0-5 and the app will tell the user whether they are right or wrong.
How would I generate a number between 0-5?
You can use any random method and performing modulo operator on it
let randomNumber = random() % 6

Random number generator confuses with srand function

I want to learn why my code is not working as I expect. I mean I want to generate a double number between 0 and 1 and I have learnt that when I use
(double)rand() / RAND_MAX, it works well. However I read that srand(time(NULL))
changes each generated random number every time I compile. However When I use them together the program generates same random number all the time. Why does this happen? Thanks.
Here is my code:
//srand(time(NULL));
number = (double)rand() / (double)RAND_MAX;
The srand() function initializes the pseudo-random number generator. You can think of that like it is pointing to the rand() a number to start its 'calculations'. Every time you compile and run your program the srand() function gives your rand() function the seed of time(NULL) (which by the way is a very big number changing every second). If you don't use the srand(), your rand() will always return the same sequence of numbers because it is given by default a standart non-changing seed (number to start the 'calculations'). You can try to give your srand() a static parameter like: srand(1500) You will see that it will return different numbers but their sequence will again be the same every time u compile and run.
For more info read here:
http://www.cplusplus.com/reference/cstdlib/srand/
http://www.cplusplus.com/reference/cstdlib/rand/

Lua math.random? [duplicate]

This question already has answers here:
Generating uniform random numbers in Lua
(4 answers)
Closed 8 years ago.
I've been having trouble generating a random number for a while now using Lua. I tried starting the script with math.randomseed(os.time()) and I was still getting the same results. How would I get the script below to generate a new random number every time I run the script?
function rand()
local x = math.random(1, #Questions) --Pick a random question from a table
return x
end
This is a well known problem. Just call math.random once or twice before using the result in your program.

set mdraws range using stata

I am using the Stata command mdraws to generate random numbers (by default using Halton sequences).
I am wondering if there's a way to set the range for the random numbers? For example, is there anything I could do using mdraws if I want my random numbers to be in the range of 0.05 to 0.5?
Elsewhere in the Stata community you are asked to explain where user-written commands you use come from, and that is a very good convention also for Stack Overflow.
mdraws is a program by Capellari and Jenkins from Stata Journal 6(2), 2006: readers will find that typing findit mdraws in Stata produces pointers to download sources.
The direct answer is No, but the problem is easily tackled by rescaling. For any variable x generated in the interval from 0 to 1, map to 0.05 to 0.5 by
gen x = 0.05 + 0.45 * y
If you have several such variables, use foreach or forval in a loop to rescale.
You didn't spell it out, so I will: this presumes that you want densities uniform on your stated interval.

Resources