1
votes

I have a plot of a function that uses rand(0) to generate numbers between 0 and 1.

I would like to set a different random sequence each time I run the Gnuplot script. The documentation says to uses rand(x) where x is a positive integer.

I have tried this

rand(floor(10*acos(rand(0))))

that gives an integer for each execution. However, this line gives me an error. I have not found any example of setting the seed of rand.

How can I set a different seed each time to get different plots?

Regards

2
What is your error? Are you just typing that to see what you get? Then you need print rand(floor(10*acos(rand(0)))). That, by the way, is just a convoluted way to get a different set of identical random numbers sequence. Quit gnuplot, start it again, and you will get this new set of random numbers again. - Dan Sp.
@DanSp. You are right. If I write rand(4) into the script, it tells me line 0: invalid command. Doing print from the interactive Gnuplot in Terminal it gives me the same number 0.99...If I run the script from the terminal, the plot is the same each run. - pablo
Anybody know that the value rand() returns when run with an argument != 0 means? - Karl
@Karl type help random in the gnuplot terminal and it will tell you. - Dan Sp.
@DanSp. No it doesn't. The return value for argument !=0 is not explained. - Karl

2 Answers

3
votes

If you want seemingly different set of random numbers each time gnuplot starts up you can seed the random number generator with the time(0) function. Use:

rand(time(0))

the first time to get it going. Then just use rand(0) for the rest in your script.

2
votes

You have to actually use (print the return value, or assign it to a variable) the rand() function with a positive integer parameter to seed the generator

print rand(-1)
print rand(0)
print rand(-1)
print rand(0)
print rand(5)
print rand(0)

rand(-1) (or restarting gnuplot) resets the seed to a standard value. What you want, I guess, is to be able to set a pseudo-random seed. The usual way to do that is to use the current time and date:

 print rand(time(0))
 plot ..... something using the rand(0) function

time() returns unix time in integer seconds if the argument is an integer, a real with ~ microsecond precision otherwise. So re-running your script with a rate < 1s will sometimes give two identical plots. You might do sth like rand(int(time(0)*1000))) to prevent that, although I couldn't imagine why that'd be necessary. ;-)