Re: random lottery draw funciton



On Fri, 10 Nov 2006 21:15:01 GMT, Kevin Williamson
<kevin.williamson4@xxxxxxxxxxxx> wrote:

I've written a lottery programme in PHP that selects 10 winners a week
from approximately 18,000 shares.

The problem I have is that of the ten winners one week the same share
number was drawn the next week and an adjacent share number the
following week (that just happened to be owned by the same member)
The program is now being looked at suspisciously.

How many times has the draw been done? Based on the information given there's
no firm evidence that anything's wrong - the people looking at it may be
falling for the Gambler's Fallacy.

The main core of the random draw simply puts all the current share
numbers into an array ($currWinner), draws a random number using the
rand function between 1 and the number of shares ($lottshares) and
returns the share id number.

for the random element ofthe program I use:

$currWinner = rand(1,$lottShares);

is this random enough?

I believe rand() uses your operating system's default rand() system call, so
the quality of the pseudo-random numbers depends on your system. Generally this
is still "random enough", but that depends on your definition. If there's money
involved, then maybe not.

There's also mt_rand() which uses the Mersenne Twister algorithm, which has
some advantages over some other pseudo-random number generators. It's still
pseudo-random, but "real" randomness generally needs hardware based on some
random physical phenomenon (thermal noise, radioactive decay, etc.)

You should test your system by setting up a simple test harness that runs the
script some large number of times and saving the results. You can then analyse
the results to see if there are any obvious groupings, or whether the
distribution is roughly uniform.

or is it possible that it needs seeding (although
I don't totally understand seeding?

That depends on your PHP version - only older versions need to be explicitly
seeded.

--
Andy Hassall :: andy@xxxxxxxxxxx :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
.



Relevant Pages

  • Re: Truly random?
    ... Computers aren't very good at generating truly ... If you need truly random numbers for cryptography, a PRNG won't ... If calling rand() or randomalone slows down ... a pseudo-random number. ...
    (comp.lang.c)
  • Re: How to generate random numbers in C
    ... Computers do not generate truly random numbers without hardware support. ... You may want pseudo-random numbers. ... process ID (but NOT in applications where real random numbers are needed, ... the return type of rand() ...
    (comp.lang.c)
  • Re: Way for computing random primes in standard C.
    ... number which is also pseudo-random? ... probabilities for all the primes that are at most 32 bits. ... Does it mean that further calls to rand() will return numbers with a new ... how is it different to calling srand() with a ...
    (comp.lang.c)
  • Re: Why will this not generate a random number!?
    ... >education is mainly based on object oriented design theory where Java ... I cannot for the life of me generate a random number. ... rand() does not generate random numbers. ... How do you know the result you got is NOT pseudo-random? ...
    (comp.lang.c)