Open Side Menu Go to the Top

09-07-2009 , 10:28 AM
Hey everyone,

My really simple question:

I can randomly generate a decimal number that's >0 & <1
I want to use this to generate a 'random card' out of a deck...

I know the answer is really simple & right in front of me, but I've spent
over a decade not doing this kind of thinking, & I'm really having trouble adjusting...

Here's my thinking, & how I'm fumbling around for the answer...

0.99999999999 / 52(virtual cards) = 0.0192307692 (I'll abbrev the numbers)

if my number = <=0.0192 ------ Ace of Clubs
if my number = >0.0192 & <=0.0384 ------ Ace of Spades
if my number = >0.0384 & <=0.0576 ------ Ace of Diamonds

etc...

do I continue this 52 times? Am I doing it the right way?

Can someone brilliant please help me think this out?

There's a follow-on question, but I want to understand fully what I'm doing on this step, or I'm going to get ridiculously mixed up.
A reeeealy simple math 'homework' problem Quote
A reeeealy simple math 'homework' problem
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
A reeeealy simple math 'homework' problem
09-07-2009 , 11:06 AM
I think this post is a better fit here than in SMP, so moved.

-Zeno
A reeeealy simple math 'homework' problem Quote
09-07-2009 , 11:23 AM
Your way works fine, but since 1/52 is not a terminating decimal you will have to round it somewhere. You will probably find that rounding these numbers makes some cards more likely than others.

I would avoid any rounding errors by discarding some random numbers, i.e.

between 0 and 0.01 - ace of clubs, between 0.01 and 0.02, two of clubs,..... between 0.51 and 0.52 king of spades... over 0.52 discard the number and generate a new random number. Keep discarding until you get a number between 0 and 0.52.
A reeeealy simple math 'homework' problem Quote
09-07-2009 , 12:58 PM
Just multiply your random number by 52, so that you get a number in [0, 52). Then just truncate (do NOT round) the decimal, and you have 52 discrete uniformly distributed values from 0 to 51.
A reeeealy simple math 'homework' problem Quote
09-07-2009 , 01:50 PM
For just 1 random card, start with an array of cards in any order, choose a random integer I from 0 to 51, and use this number to select the card at offset I into the array.

If you want a shuffling routine, which generates 52 random cards, start with a deck (array) of cards in any order, and for each of the first I=51 places (I=0 to 50), generate a random integer J between I and 51, and swap the card in position I with the card in position J. Note that each card must be able to swap with itself. Here is some Perl code that does this:

Code:
@deck = (As,Ks,Qs,Js,Ts,9s,8s,7s,6s,5s,4s,3s,2s,Ah,Kh,Qh,Jh,Th,9h,8h,7h,6h,5h,4h,3h,2h,Ad,Kd,Qd,Jd,Td,9d,8d,7d,6d,5d,4d,3d,2d,Ac,Kc,Qc,Jc,Tc,9c,8c,7c,6c,5c,4c,3c,2c);

for ($i=0; $i<=50; $i++) {    # For 51 cards, and the 52nd is automatic
  $j = $i + int(rand(52-$i)); # $j is integer from $i to 51 
  $temp = $deck[$j];          # Get card at offset of $j
  $deck[$j] = $deck[$i];      # Swap card at offset of $j
  $deck[$i] = $temp;          # ...with next card
}

Be sure to seed the random number generator with something sufficiently unpredictable if this is important to you.

Last edited by BruceZ; 09-07-2009 at 04:43 PM.
A reeeealy simple math 'homework' problem Quote
09-07-2009 , 06:55 PM
Quote:
Originally Posted by ingjald10
Just multiply your random number by 52, so that you get a number in [0, 52). Then just truncate (do NOT round) the decimal, and you have 52 discrete uniformly distributed values from 0 to 51.
this
A reeeealy simple math 'homework' problem Quote
09-07-2009 , 07:14 PM
Quote:
Originally Posted by BruceZ
If you want a shuffling routine, which generates 52 random cards, start with a deck (array) of cards in any order, and for each of the first I=51 places (I=0 to 50), generate a random integer J between I and 51, and swap the card in position I with the card in position J.
Or if you are Planet Poker, generate a random integer between 1 and 51, rather than I and 51, then do the swap
A reeeealy simple math 'homework' problem Quote
09-08-2009 , 12:20 AM
Quote:
Originally Posted by BruceZ
For just 1 random card, start with an array of cards in any order, choose a random integer I from 0 to 51, and use this number to select the card at offset I into the array.

If you want a shuffling routine, which generates 52 random cards, start with a deck (array) of cards in any order, and for each of the first I=51 places (I=0 to 50), generate a random integer J between I and 51, and swap the card in position I with the card in position J. Note that each card must be able to swap with itself. Here is some Perl code that does this:

Code:
@deck = (As,Ks,Qs,Js,Ts,9s,8s,7s,6s,5s,4s,3s,2s,Ah,Kh,Qh,Jh,Th,9h,8h,7h,6h,5h,4h,3h,2h,Ad,Kd,Qd,Jd,Td,9d,8d,7d,6d,5d,4d,3d,2d,Ac,Kc,Qc,Jc,Tc,9c,8c,7c,6c,5c,4c,3c,2c);

for ($i=0; $i<=50; $i++) {    # For 51 cards, and the 52nd is automatic
  $j = $i + int(rand(52-$i)); # $j is integer from $i to 51 
  $temp = $deck[$j];          # Get card at offset of $j
  $deck[$j] = $deck[$i];      # Swap card at offset of $j
  $deck[$i] = $temp;          # ...with next card
}

Be sure to seed the random number generator with something sufficiently unpredictable if this is important to you.
I just want to add that what BruceZ describes is called a Knuth shuffle and that one must be very careful in implementing any kind of shuffling routine, since it is very easy to make subtle errors that introduce a bias. Shuffle routines are much more treacherous than most people suppose. For a little about that, the following link discusses this:

http://en.wikipedia.org/wiki/Shuffli...ing_algorithms

The Knuth shuffle is fine for most purposes, provided a suitable random number generator is used and even a very good random number generator will introduce a miniscule bias, which in most cases is tiny enough to be acceptable for all practical purposes.

In short, do not presume this is a simple task if you want to do it properly.

Last edited by R Gibert; 09-08-2009 at 12:49 AM.
A reeeealy simple math 'homework' problem Quote
09-08-2009 , 02:34 AM
I definitely THOUGHT it was a simple task, but yea I see just how complicated this can get very quickly. Lol I'm just learning the very basics of javascript, & was trying to figure out the math behind drawing a simple random card, with a view to making my own blackjack game as a hobby...

Think I might just put that idea on the backburner till my skills increase...

Thanks for your help tho everyone, I ended up using pyro's simple explanation to make myself a program that draws a single random card... Preeeeety interesting huh? lol I love it!

BruceZ's answer looked promising to me too. I think when I finally do one day make this game it's going to have to use arrays... The simple program I wrote uses like 52 if statements to choose a single card lolz

Last edited by floppynutz; 09-08-2009 at 02:40 AM.
A reeeealy simple math 'homework' problem Quote
09-09-2009 , 04:16 PM
Quote:
Originally Posted by ingjald10
Just multiply your random number by 52, so that you get a number in [0, 52). Then just truncate (do NOT round) the decimal, and you have 52 discrete uniformly distributed values from 0 to 51.
This will work very well (do not round)
A reeeealy simple math 'homework' problem Quote
A reeeealy simple math 'homework' problem
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
A reeeealy simple math 'homework' problem

      
m