Active Topics

 


Reply
Thread Tools
Posts: 692 | Thanked: 264 times | Joined on Dec 2009
#1
So as part of a fancy shellscript I'm making, I need to generate a random number that will accept a custom seed value, and output a "random" number within a custom range starting from 0. But that random number needs to be totally predictable based on the seed, so that if I run the script on another machine with the same inputs it'll get the same result. I've tried this:

Code:
awk 'BEGIN { srand($seedvalue); printf("%d\n",rand()*$max)
...but I'm getting different results on different PCs. On one PC the results are predictable but something here is accepting a seed other than $seedvalue that varies from one PC to the next.

The script can only use standard GNU tools included on even minimalistic distros.

Any ideas?
__________________
"Impossible is not in the Maemo vocabulary" - Caballero
 
danramos's Avatar
Posts: 4,672 | Thanked: 5,455 times | Joined on Jul 2008 @ Springfield, MA, USA
#2
Originally Posted by GameboyRMH View Post
So as part of a fancy shellscript I'm making, I need to generate a random number that will accept a custom seed value, and output a "random" number within a custom range starting from 0. But that random number needs to be totally predictable based on the seed, so that if I run the script on another machine with the same inputs it'll get the same result. I've tried this:

Code:
awk 'BEGIN { srand($seedvalue); printf("%d\n",rand()*$max)
...but I'm getting different results on different PCs. On one PC the results are predictable but something here is accepting a seed other than $seedvalue that varies from one PC to the next.

The script can only use standard GNU tools included on even minimalistic distros.

Any ideas?
Among an ARRAY of other things that will make this difficult, so long as the operating system uses an entropy engine that doesn't reset with every boot (i.e. the current state is saved before reboot or shutdown, so you can get a relatively more chaotic result every reboot at any particular time), you're going to have a difficult time with this. You'll need to reset the entropy engine on each PC (i.e. make it stop saving entropy) before rebooting to try to force predictable numbers from similar hardware--and that's only if they realy ARE using similar random number generating hardware (usually a white noise generator). Good luck, though.
__________________
Nokia's slogan shouldn't be the pedo-palmgrabbing image with the slogan, "Connecting People"... It should be one hand open pleadingly with another hand giving the middle finger and the more apt slogan, "Potential Unrealized." --DR
 
Posts: 2,802 | Thanked: 4,491 times | Joined on Nov 2007
#3
Originally Posted by GameboyRMH View Post
Code:
awk 'BEGIN { srand($seedvalue); printf("%d\n",rand()*$max)
Unfortunately different awk interpreters use different number generators so that wouldn't be predictable.

The script can only use standard GNU tools included on even minimalistic distros.
If you really had GNU tools (eg bash or gawk) that would be easy... Is perl an option?
 
Posts: 1,680 | Thanked: 3,685 times | Joined on Jan 2011
#4
Originally Posted by GameboyRMH View Post
...But that random number needs to be totally predictable based on the seed...
So let me get this right, you want a random number that is not random?

Random numbers are intrinsically unpredictable. What you are asking for is a non random random number generator. A number generator if you will.

http://en.wikipedia.org/wiki/List_of...rs_.28PRNGs.29
__________________
N900: One of God's own prototypes. A high-powered mutant of some kind never even considered for mass production. Too weird to live, and too rare to die.

Last edited by vi_; 2011-08-01 at 07:40.
 
Posts: 692 | Thanked: 264 times | Joined on Dec 2009
#5
Any software RNG is 100% predictable if you can control all the inputs

Yeah I'm ready to try solutions that involve perl or python scripts now...
__________________
"Impossible is not in the Maemo vocabulary" - Caballero
 
Posts: 692 | Thanked: 264 times | Joined on Dec 2009
#6
Found a way to do it in python:

Code:
python -c "exec(\"import random; random.seed(999); x=random.randint(0,10); print x;\")" | tr -d '\n'
In this example 999 is the seed value and the output range is 0-10.
__________________
"Impossible is not in the Maemo vocabulary" - Caballero
 
Reply

Tags
entropy, random


 
Forum Jump


All times are GMT. The time now is 08:11.