Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    Shellscripting help: Predictable random numbers

    Reply
    GameboyRMH | # 1 | 2011-08-01, 03:56 | Report

    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?

    Edit | Forward | Quote | Quick Reply | Thanks

     
    danramos | # 2 | 2011-08-01, 05:22 | Report

    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.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    lma | # 3 | 2011-08-01, 05:50 | Report

    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.

    Originally Posted by
    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?

    Edit | Forward | Quote | Quick Reply | Thanks

     
    vi_ | # 4 | 2011-08-01, 07:33 | Report

    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

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by vi_; 2011-08-01 at 07:40.

     
    GameboyRMH | # 5 | 2011-08-01, 13:08 | Report

    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...

    Edit | Forward | Quote | Quick Reply | Thanks

     
    GameboyRMH | # 6 | 2011-08-01, 14:43 | Report

    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.

    Edit | Forward | Quote | Quick Reply | Thanks

     
vBulletin® Version 3.8.8
Normal Logout