473,657 Members | 2,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = "00001000000000 0 ..."

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.
Jul 17 '05 #1
14 3139
"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 = "00001000000000 0 ..."

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
Jul 17 '05 #2
>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 = "00001000000000 0 ..."

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
Jul 17 '05 #3
I noticed that Message-ID: <ce********@lib rary1.airnews.n et> 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/
Jul 17 '05 #4
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.
Jul 17 '05 #5
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 = "00001000000000 0 ..."

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
Jul 17 '05 #6
"Geoff Berrow" <bl******@ckdog .co.uk> wrote in message
news:ov******** *************** *********@4ax.c om...
I noticed that Message-ID: <ce********@lib rary1.airnews.n et> 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.
Jul 17 '05 #7

"Pjotr Wedersteers" <x3****@westert erp.com> wrote in message
news:41******** *************** @dreader16.news .xs4all.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 = "00001000000000 0 ..."

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.
Jul 17 '05 #8
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.
Jul 17 '05 #9
In article <41************ *********@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>
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6239
by: vishal | last post by:
i want to generate a random number of a fixed length so how can i do this ?? i know some function which returns a single random character at a time but is there any built-in function which takes length of string and return a random string of that length?????? can i implement mechanism so that the random number generated is form a list of characters i specify. e.g. suppose if i want to create a random
6
2569
by: Jean-Fran?ois Lacrampe | last post by:
Hello, I've got two random number/statistics questions I'd like you to review. My first question is not directly related to PHP, but will be implemented in PHP, as explained in my second question, so let's go: I want to generate 10000 strings of x characters, with one chance (or less) on a million that you can guess them by just randomly typing them. So I need to know what is the value of x.
23
1766
by: MConly | last post by:
Can you tell me what happens inside CPU when I rand() ? Where can I find the true rand function implemented ? I have heard that rand() in C/C++ is n't a good one but why it isn't a good one, are they lying to me ? Why do they have to do that ? Thank you MnConly
4
2202
by: anita | last post by:
I had posted this question before, but did not hear from anybody. Can somebody pls help me out. I am creating a table with two fields F1, F2 and F2 has about 50,000 randomly generated alphanumeric nos(RandomID)stored in the combobox control. Everytime I enter a new record in F1, the next available RandomID should be automatically assigned to the new record entry and displayed in the respective form control(F2). Can anybody guide me...
13
4224
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number between 0 and 100? (call rand() only 10 times...) Thanks, qq
6
4688
by: Starbuck | last post by:
Hi In VB6 we used the following to create a unique random number - Function longSerial() As Long longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer * 100), "0000000")) longSerial = longSerial Xor Int(2147483647 * Rnd)
11
12806
by: TreatmentPlant | last post by:
I need to generate a few thousand true random numbers using C++. I have some code now that does alright, but when you plot the results on a graph, you notice patterns, so the numbers are not truly random, but pseudo-random. I tried randomly seeding the seed! to see if that improved things; it didn't. Can anyone help?
22
3429
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
13
2800
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num )
11
3017
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays of values. I get a random index value for each array so I can pull the data from them.
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.