Random Number Question

This is a discussion on Random Number Question within the PHP Language forums, part of the PHP Programming Forums category; Hi, Is there a way to repeat a set of code until a certain if statement is satisfied. If it ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-30-2004
Paul C-T
 
Posts: n/a
Default Random Number Question

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.


Reply With Quote
  #2 (permalink)  
Old 07-30-2004
steve
 
Posts: n/a
Default Re: Random Number Question

"Paul C-T" wrote:
> 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, Yes, can be done. You make an array of all the cards (n cards).
You pick a random number between 1 and n. When a number is picked,
you simply take the card out of the array, so now you have one less
card in the array. So now you pick from 1 to (n-1) and so on.

You can use php’s rand function.

Also you can unset any item in the array. The problem would be that
the indecis would not reorganize, and e.g. when you remove item a,
item (a+1) would still have the same index.

So even though there is probably a better solution (anyone?) you can
do:
$my_array = array_merge( array(), $my_array);
which reorganizes the index, and you can continue iterating.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-Random-N...ict134850.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=450349
Reply With Quote
  #3 (permalink)  
Old 07-30-2004
Gordon Burditt
 
Posts: n/a
Default Re: Random Number Question

>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?


This is generally called a while loop. Depending on your exact
wording, you may need to negate the condition.

>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 ...


This can take a LOT of time especially when you get to the point
where you have only 2 cards left in the deck, and it gets even worse
when you use multiple "standard" 52-card decks.

Random shuffling is usually done like this:

Make an array with indexes 0 .. N-1 for N cards. Populate the array.
Select a card by selecting a random integer between 0 and N-1 inclusive.
This is the index of your chosen card. Now swap that card in the
array with card N-1, then decrement N. Repeat to draw more cards until
you don't need any more, or you run out of cards (N == 0).

Gordon L. Burditt
Reply With Quote
  #4 (permalink)  
Old 07-30-2004
Geoff Berrow
 
Posts: n/a
Default Re: Random Number Question

I noticed that Message-ID: <cecnbp$md5@library1.airnews.net> from Gordon
Burditt contained the following:

>Random shuffling is usually done like this:
>
>Make an array with indexes 0 .. N-1 for N cards. Populate the array.
>Select a card by selecting a random integer between 0 and N-1 inclusive.
>This is the index of your chosen card. Now swap that card in the
>array with card N-1, then decrement N. Repeat to draw more cards until
>you don't need any more, or you run out of cards (N == 0).



shuffle() and array_rand() also seem useful.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Reply With Quote
  #5 (permalink)  
Old 07-30-2004
Paul C-T
 
Posts: n/a
Default Re: Random Number Question

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.


Reply With Quote
  #6 (permalink)  
Old 07-30-2004
Pjotr Wedersteers
 
Posts: n/a
Default Re: Random Number Question

Paul C-T wrote:
> 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.

Ah, finally someone doing something I know a bit about, lol!
Paul, there are some things you may want to consider. First of all,
shuffling can be done easy using shuffle (), which auto-shuffles your array
of cards. Then just deal from the top or bottom. There is also the Mersenne
Twister Random function (quicker) you can use. Check php manual for that.
For home use the shuffle () function is very comfortable in use.

But there is a catch when dealing and shuffling cards. A deck of cards can
be stacked in 2^52 ways. The default randomizer function is NOT capable of
doing all these permutations, simply because its seed and reach is too low.
So unless you are doing this just for fun, beware your randomly shuffled
deck is not exactly like having a real deck of cards. Also be careful with
the predictability. Cardplaying sites use all kinds of true random input
(mouse movement etc) from their users to seed the randomizers.

There are better/faster randomizers available online, including ones hat
have a range broader then 2^52. Not sure if they are available for PHP btw,
but you could always use an external randomizer if need be using exec()
Need more, just holler!
Pjotr


Reply With Quote
  #7 (permalink)  
Old 07-31-2004
Chung Leong
 
Posts: n/a
Default Re: Random Number Question

"Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
news:ovsjg01gmul0msm90hbhtii8vsppnjv7ti@4ax.com...
> I noticed that Message-ID: <cecnbp$md5@library1.airnews.net> from Gordon
> Burditt contained the following:
>
> >Random shuffling is usually done like this:
> >
> >Make an array with indexes 0 .. N-1 for N cards. Populate the array.
> >Select a card by selecting a random integer between 0 and N-1 inclusive.
> >This is the index of your chosen card. Now swap that card in the
> >array with card N-1, then decrement N. Repeat to draw more cards until
> >you don't need any more, or you run out of cards (N == 0).

>
>
> shuffle() and array_rand() also seem useful.
> --
> Geoff Berrow (put thecat out to email)
> It's only Usenet, no one dies.
> My opinions, not the committee's, mine.
> Simple RFDs http://www.ckdog.co.uk/rfdmaker/


Shuffle() is the right one to use in this case. Just shuttle the array then
pop the items off one at a time with array_pop(). That mimicks exactly what
happens when you're dealing cards. Array_rand() wouldn't work since it
doesn't prevent the same item being picked again.


Reply With Quote
  #8 (permalink)  
Old 07-31-2004
Chung Leong
 
Posts: n/a
Default Re: Random Number Question


"Pjotr Wedersteers" <x33159@westerterp.com> wrote in message
news:410a1d69$0$30782$e4fe514c@dreader16.news.xs4a ll.nl...
> Paul C-T wrote:
> > 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.

> Ah, finally someone doing something I know a bit about, lol!
> Paul, there are some things you may want to consider. First of all,
> shuffling can be done easy using shuffle (), which auto-shuffles your

array
> of cards. Then just deal from the top or bottom. There is also the

Mersenne
> Twister Random function (quicker) you can use. Check php manual for that.
> For home use the shuffle () function is very comfortable in use.
>
> But there is a catch when dealing and shuffling cards. A deck of cards can
> be stacked in 2^52 ways. The default randomizer function is NOT capable of
> doing all these permutations, simply because its seed and reach is too

low.
> So unless you are doing this just for fun, beware your randomly shuffled
> deck is not exactly like having a real deck of cards. Also be careful with
> the predictability. Cardplaying sites use all kinds of true random input
> (mouse movement etc) from their users to seed the randomizers.


Errr, isn't that as good as casinos asking their patrons to shuffle the
cards?

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.


Reply With Quote
  #9 (permalink)  
Old 07-31-2004
Pjotr Wedersteers
 
Posts: n/a
Default Re: Random Number Question

Chung Leong wrote:

>> Ah, finally someone doing something I know a bit about, lol!
>> Paul, there are some things you may want to consider. First of all,
>> shuffling can be done easy using shuffle (), which auto-shuffles
>> your array of cards. Then just deal from the top or bottom. There is
>> also the Mersenne Twister Random function (quicker) you can use.
>> Check php manual for that. For home use the shuffle () function is
>> very comfortable in use.
>>
>> But there is a catch when dealing and shuffling cards. A deck of
>> cards can be stacked in 2^52 ways. The default randomizer function
>> is NOT capable of doing all these permutations, simply because its
>> seed and reach is too low. So unless you are doing this just for
>> fun, beware your randomly shuffled deck is not exactly like having a
>> real deck of cards. Also be careful with the predictability.
>> Cardplaying sites use all kinds of true random input (mouse movement
>> etc) from their users to seed the randomizers.

>
> Errr, isn't that as good as casinos asking their patrons to shuffle
> the cards?
>
> 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.


Reply With Quote
  #10 (permalink)  
Old 07-31-2004
Tim Van Wassenhove
 
Posts: n/a
Default Re: Random Number Question

In article <410b8543$0$566$e4fe514c@news.xs4all.nl>, Pjotr Wedersteers wrote:
> Chung Leong wrote:
>
>>> Ah, finally someone doing something I know a bit about, lol!
>>> Paul, there are some things you may want to consider. First of all,
>>> shuffling can be done easy using shuffle (), which auto-shuffles
>>> your array of cards. Then just deal from the top or bottom. There is
>>> also the Mersenne Twister Random function (quicker) you can use.
>>> Check php manual for that. For home use the shuffle () function is
>>> very comfortable in use.
>>>
>>> But there is a catch when dealing and shuffling cards. A deck of
>>> cards can be stacked in 2^52 ways. The default randomizer function
>>> is NOT capable of doing all these permutations, simply because its
>>> seed and reach is too low. So unless you are doing this just for
>>> fun, beware your randomly shuffled deck is not exactly like having a
>>> real deck of cards. Also be careful with the predictability.
>>> Cardplaying sites use all kinds of true random input (mouse movement
>>> etc) from their users to seed the randomizers.

>>
>> Errr, isn't that as good as casinos asking their patrons to shuffle
>> the cards?
>>
>> 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.


I've found network traffic dumps to be more than random enough to seed the
randomizer ;)

--
Tim Van Wassenhove <http://home.mysth.be/~timvw>
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:43 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0