Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Random numbers generator

by Anonymous Monk
on Feb 07, 2001 at 19:26 UTC ( #56967=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need a snipet of code that will generate a random 1 digit number. It needs to be completely random and if run on 3 different machines would produce three different results each time.
If anyone knows how to do this let me know,
Thank you,
JBachini@yahoo.co.uk

Comment on Random numbers generator
(kudra: use search) Re: Random numbers generator
by kudra (Vicar) on Feb 07, 2001 at 19:31 UTC
    In the future, you might consider doing a simple search to see if your question has already been asked. Looking for 'random number' or 'random numbers', for example, will give you a couple of solutions to consider.
Re: Random numbers generator
by davorg (Chancellor) on Feb 07, 2001 at 19:33 UTC

    Well, 'completely random' might be a bit of a problem, but if you'll settle for 'pseudo random' then this will do the trick:

    my $random = int rand 10;
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      It is just going to be a lot more difficult to ensure that if you run it on different machines it returns different results ;--)

      Maybe putting the result in an LWP accessible place, retrieving them and doing:

      my $random = int rand 10 while (($random != $previous_result1) && ($ra +ndom != $previous_result2)); print LWP_ACCESSIBLE $random;

      Or maybe the problem was not exactly properly stated...

      davorg is right. Computers (as they now exist) can never produce a truly random number. This is because, as Bruce Schneier puts it, "Computers are deterministic beasts: stuff goes in on one end, completely predictable operations occur inside, and different stuff comes out the other end." This enables back-computation.

      Computers need humans to be truly random. They do this by having humans entropize their entropy pool with mouse movements, thus enabling the creation of Truly Random numbers.

      redmist
      Silicon Cowboy
Re: Random numbers generator
by mr.nick (Chaplain) on Feb 07, 2001 at 19:38 UTC
    You also might want to try Crypt::Random for a much better random number generator. I don't believe the thread that was mentioned on covering this before comments on it's existence.
Re: Random numbers generator
by arhuman (Vicar) on Feb 07, 2001 at 19:55 UTC
    "Anyone trying to achieve true randomness with an automatic machine is a fool".
    (It's a bad French->English translation from an old quote I remember...)

    Seriously, if you want to make effective pseudo random generator you'll have to find a way to get 'chaotic' seed values (keyboard timer,time,uptime...) some OS provide (quite) good sources (who said /dev/random /dev/urandom ?) but the 'real' randomness could only be reached by dedicated special hardware.

    Now for the algorithm the most used is linear congruential generator n+1=(n*a+b)%c but there are a lot others; I recommend you any book on crypto (the SCHNEIER or the STINSON) for they will explain you the weaknesses/strength of all algorithms...

    But if as I think, you just want something simple : the linear congruential generator with a,b,c values defined according to the RFC 1750 and an initial seed based on several pseudo-random factors (uptime,checksum on MAC address,delay between keystroke) should be fast and random enough.

Re: Random numbers generator
by Anonymous Monk on Feb 07, 2001 at 20:20 UTC
    I believe that, for diffrent results on diffrent machine, you can base your random generation upon a timer..
    do depending on the time of your server the random number is generated.
    The timer is the same once each day.. :)
    I've done this a lot in VB dunno how in Perl..
    # VB Code in here RANDOMIZE(Timer) N = INT(RND * 10)
    I will be flamed.. :)
      Have a look at srand and rand. That block translates pretty well into perl:
      srand; my $n=int(rand(10)); # gets you a number 0..9

      --
      Me spell chucker work grate. Need grandma chicken.

        srand is only required if your script is running on a version of Perl earlier than 5.004.

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://56967]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-09-02 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (20 votes), past polls