Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
more useful options
 
PerlMonks  

Re: generate random number without using any built in function in perl?

by kennethk (Monsignor)
on May 08, 2013 at 20:08 UTC ( #1032670=note: print w/ replies, xml ) Need Help??


in reply to generate random number without using any built in function in perl?

Not to be obtuse, but why can't you use rand? This smacks of an XY Problem. There are a myriad of ways to generate pseudom random series, and the appropriate solution depends very strongly on the intended purpose. 'Best time complexity' is solved by xkcd://221, which is probably not what you mean. Most Linux boxes have /dev/random at their disposal for truly high quality randomness, but this is overkill for nearly every application, and denies the possibility of playback.

~$ perl -e 'open $rnd, "<", "/dev/random"; read $rnd, $value, 4; prin +t "Value = ", unpack ("H*", $value), "\n"' Value = 0de13803
Equivalent functionality can be garnered on other systems with TrulyRandom. These are very slow, though still 'Best time complexity'.

Describe your application, and the quality of the guidance we can provide will improve dramatically.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re: generate random number without using any built in function in perl?
Select or Download Code
Re^2: generate random number without using any built in function in perl?
by AnomalousMonk (Monsignor) on May 08, 2013 at 21:38 UTC

    In all obtuseness, shouldn't the comment in the xkcd code be "guaranteed to have been random"?

Re^2: generate random number without using any built in function in perl?
by Laurent_R (Priest) on May 08, 2013 at 22:08 UTC

    Not to be obtuse, but why can't you use rand? This smacks of an XY Problem.

    Yes, I fully agree.

    To the OP: please tell us why you can't (or don't want to) use the Perl random number generator, which is, I think, of a high quality and has been well tested over many years, and would want to go for something else, which is likely to work much less properly unless you are willing to commit huge amount of time to test how well it performs.

    I once made a very basic pseudo random number generator, because I did not have any library available to do that for me in the environment that I was using. It ended up working for the purpose that I had, because the requirement was really not demanding. But trying to do that, you figure out that this is a very difficult problem, and sometimes very clever ideas fail miserably. (I knew it was quite difficult before I started, but I did not realize how much more difficult than I thought it was.)

    I would suggest that you take a look at Donald Knuth's "The Art of of Computer Programming", Volume 2, Seminumerical Algorithms, Chapter 3, Random Numbers. Once you've read through the 190 pages (in the edition that I own) devoted to random numbers, I think that you will agree that you probably don't want a "quick fix" solution to such a complicated problem. Using a well tested library is far safer in probably more than 99.99% of the cases.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-04-18 01:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls