This is a discussion on Random Number Question within the PHP Language forums, part of the PHP Programming Forums category; On Sat, 31 Jul 2004 13:40:55 +0200, "Pjotr Wedersteers" <x33159@westerterp.com> wrote: >&...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
On Sat, 31 Jul 2004 13:40:55 +0200, "Pjotr Wedersteers" <x33159@westerterp.com>
wrote: >> Modern CPUs have hardware random number generator built-in so it >> seems a better idea to hit /dev/random when you need a truely random >> number. > >True random generator in a computer ? Me thinks that's a bit of an >impossibility, unless it takes in analog signals from somewhere. >And the problem is the seeding of the (pseudo) randomizer plus the fact most >randomizers don't cover the full range to 2^52. Hardware RNG's, at least on Intel chips, use "thermal noise" to generate their values, making them pretty reasonably random. They're certainly not pseudo-random based off an algorithm, they're based on a randomly fluctuating physical property. -- Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space |
|
|||
|
Andy Hassall wrote:
> On Sat, 31 Jul 2004 13:40:55 +0200, "Pjotr Wedersteers" > <x33159@westerterp.com> wrote: > >>> Modern CPUs have hardware random number generator built-in so it >>> seems a better idea to hit /dev/random when you need a truely random >>> number. >> >> True random generator in a computer ? Me thinks that's a bit of an >> impossibility, unless it takes in analog signals from somewhere. >> And the problem is the seeding of the (pseudo) randomizer plus the >> fact most randomizers don't cover the full range to 2^52. > > Hardware RNG's, at least on Intel chips, use "thermal noise" to > generate their values, making them pretty reasonably random. They're > certainly not pseudo-random based off an algorithm, they're based on > a randomly fluctuating physical property. That's cool! (or hot). Indeed, if analog data is the randomizer source, true randomness can be had. Any idea on the range of this randomizer ? Or links to good in depth articles ? Thanks! Very happy with this, if I can use that I can throw the soundcardnoise/input based randomizer out! Pjotr |
|
|||
|
I can share with you the code i use for my random routine (isnt perfect but
works) ;) here it goes..... // FUNCTION TO TRY AND MAKE A BETTER RANDOM NUMBER function make_seed() { list($usec, $sec) = explode(' ', microtime()); return (float) $sec + ((float) $usec * 100000); } $solucao = 0 $options = 1 srand((double)microtime()*intval(rand(1,1000000))) ; $respostas[1] = rand (1,52); $solucao++; while ($solucao <= $options) { srand(make_seed()); $pergunta = rand(1, 52); if (!in_array($pergunta, $respostas)) { $respostas[$solucao] = $pergunta; $solucao++; } } print_r ($respostas); // PRINTS THE ARRAY WITH UNIQUE CARDS ALREADY ADDED ************************************************** * explanation: ************************************************** * you say how many cards ($options) you want to add to the already picked cards in the example 1 -- ZXSpectrum "Name The Game" - Can you guess the name of ZXSpectrum games, just by looking at a ingame picture? Well you can find out if you can (or not) at.... http:/ntg.docaj.net -- |
|
|||
|
Also, I didn't see this mentioned...
rather than doing rand(1,52) each time through the loop and subsequent multiple attempts to "draw" a random number that hasn't already been picked, why not do a "real world" approach and only pick a random num from what is avail? have a $current_deck array or $cards_left array.. after you "draw" a card: unset($current_deck[card_x]); you can get a random card from this deck with $rand_card = array_rand($current_deck); complete psudo code: while ( sizeof($current_deck) > 0 ) { // this code is executed exactly 52 times $rand_card = array_rand($current_deck); unset($current_deck[$rand_card]); } "Paul C-T" <paulcharltonthomson@hotmail.com> wrote in message news:<4109b542$0$63371$ed2e19e4@ptn-nntp-reader04.plus.net>... > Hi, > > Is there a way to repeat a set of code until a certain if statement is > satisfied. If it is then to exit the loop and if not repeat the code? > > Say I want to write a card game application in PHP and I want to chose a > card from the deck $card1 = rand(1,52); gets me my first card. > > I need to record which card has been dealt so I have a variable $dealt which > starts out as a string of 52 zeros. I update the position of $card1 in the > $dealt string to "1". > > So the 5 of Spades makes $dealt = "000010000000000 ..." > > I want to choose another card so I use $card2 = rand(1,52); and then need to > check if it has already been dealt. If the position in $dealt is a 1 then I > need to repeat the random number code until it is a 0 when I can update it > to a 1 and move on ... > > Or is there a better way of doing this sort of thing ?? > > Help, as always, appreciated. > > Paul. |
|
|||
|
Paul C-T wrote:
> Thanks for the advice ... seems like I need to do a bit more > flowcharting of the problem ... and investigate some new PHP function. > > :-) > > Much obliged. > > Paul. Yw. Some nice background on real-life issues for poker-sites (applies to any similar gametype of course) Quite a good explanation: http://www.bugsysclub.com/club/about/integrity.htm |