Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Srand versus rand

by extremely (Priest)
on Feb 16, 2001 at 12:39 UTC ( #58816=note: print w/replies, xml ) Need Help??


in reply to Srand versus rand
in thread Pi calculator

Nope, letting perl set srand is good enough. And you are right about Monte Carlo needing a purer random base than a pseudo-random generator. Most Monte Carlo's use scads of randoms per cycle and you loop the psuedo random in 2**31 calls on most systems and 2**15 on some.

Look to CPAN and you will find some of what you need. First off, if you are going to write your own "random" sequence generator you my find PDL handy. If you want someone else to do the work on random numbers try Math::Random or Math::TrulyRandom but I would recommend you find an OS specific random source like Linux's /dev/random and /dev/urandom

#!/usr/bin/perl -w use strict; #use Linux; ## I wish. =) (yeah yeah, $^O, etc etc) open UR, "</dev/urandom" or die "Oh man, your system sucks, $!"; my $pages= shift()+0 || 1; die "Give me a number greater than 0 or nothing, bub.\n" unless $pages +>0; while ($pages-->0) { my $buf; read UR, $buf, 512 or die "Ouch that shouldn't happen, $!"; for (0..31) { print vec($buf,$_*4,32), "\t", vec($buf,$_*4+1,32), "\t", vec($buf,$_*4+2,32), "\t", vec($buf,$_*4+3,32), "\n"; } }

--
$you = new YOU;
honk() if $you->love(perl)

Replies are listed 'Best First'.
Re: Re: Srand versus rand
by Fingo (Monk) on Feb 16, 2001 at 16:12 UTC
    It shoulden't matter if there are repeats or not. As long as they do not apper in the same order, it woulden't really matter since I am looking to see if A^2 + B^2 is below 1. Actualy if I make 2 pairs of numbers one pair that A^2 + B^2 is less than 1, and another where it is not. Than I can generate the order in which they appear by some random factor (take a random number and each digit is used to see which of the 2 pairs I choose depending on if it's even or not). UPDATE: I now realize what I said was wrong randomly generating true or false will not generate Pi. Random numbers are sometimes very hard to visualize
      It shouldn't matter if there are repeats or not if there is a uniform bias in the random number generator. However most pseudo-random number generators fail this criteria (ones in practice, in theory they often pass :) ).

      What I was proposing you use are sometimes called quasi-random numbers. This is because they are not really too random at all. They have a heavy bias against being chosen twice. The advantage here is that you converge faster. Additionally in the real world they will be more likely to be correct (since any unatural skew in the bias would have to be tempered with the non-repeating bias).

      The reason it converges faster is because the numbers chosen are forced to not repeat, but still spread themselves evenly through a given space. I hope you can see why this would converge more quickly -- it's a tad hard to draw an ascii picture to represent this.

      Basically it's like using an iteratively increasing grid (like my last post), but then you do not have to commit to any particular number of samples (with the grid, you have to do 4 the first iteration, then 5 more the next, then 10 the next, etc).

      I have to be off to work, but as an example here is a base two Hamilton sequence: 0.0, 0.1, 0.01, 0.11, 0.001, 0.101, 0.011, and 0.111 . You may want to convert these numbers into decimal so you can see the sequence: 0.000, 0.500, 0.250, 0.750, 0.125, 0.625, 0.375, 0.875 . Normally, you would use a higher base (which works best when the base is also a prime number), but using 2 lets you see how it is slowly filling up a grid for you. Sobol sequences produce more random results than Hamilton, so using them is more ideal. But anyway, you get the picture.

      Ciao,
      Gryn

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2021-06-16 18:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (76 votes). Check out past polls.

    Notices?