wrragsdale
wrragsdale
  • Threads: 4
  • Posts: 25
Joined: Mar 7, 2011
March 9th, 2011 at 8:47:43 AM permalink
How do Random Number Generators deduce the outcomes of 2 six-sided dice rolls?
MathExtremist
MathExtremist
  • Threads: 88
  • Posts: 6526
Joined: Aug 31, 2010
March 9th, 2011 at 8:53:58 AM permalink
An RNG is just an algorithm for producing pseudorandom sequences of bits. Those bits can be interpreted as integers, typically in a range [0, 2^N). In order to turn such a random integer into a die, you'd just scale the output using the modulus function. There are several known methods to do this. For a six-sided cie (range 0-5) all you need is 3 bits anyway.

The fair way to do this is to take 3 random bits, make sure they're not 6 or 7, and then use the value as a die roll. Do this twice and you have your roll of two dice.
"In my own case, when it seemed to me after a long illness that death was close at hand, I found no little solace in playing constantly at dice." -- Girolamo Cardano, 1563
wrragsdale
wrragsdale
  • Threads: 4
  • Posts: 25
Joined: Mar 7, 2011
March 9th, 2011 at 9:32:28 AM permalink
Woah, brother, you are way above my pay grade...can you put that into layman's terms???
DJTeddyBear
DJTeddyBear
  • Threads: 210
  • Posts: 11060
Joined: Nov 2, 2009
March 9th, 2011 at 10:13:51 AM permalink
Layman's terms?

He gave you the technical answer to how a random number generator works - to pick ONE number. In there, he slipped in how to pick two dice.

The simple answer is this: You have the RNG generate a number from 1 to 6 - twice.
I invented a few casino games. Info: http://www.DaveMillerGaming.com/ ————————————————————————————————————— Superstitions are silly, childish, irrational rituals, born out of fear of the unknown. But how much does it cost to knock on wood? 😁
odiousgambit
odiousgambit
  • Threads: 327
  • Posts: 9734
Joined: Nov 9, 2009
March 9th, 2011 at 10:18:18 AM permalink
sometimes is just the jargon

algorithm = a formula [pretty much]. It's a formula that generates a number that, for the next number, doesnt have a recognizable pattern. However, if you discover the formula, you would be able to predict the next number it seems.

Let's say your algo. generate numbers 0-999. Now divide 1000 by 6 and you get 166.67. So zero to that number can be '1' on the die. If the algo. generates 167 to 333 you simulate a roll of '2', etc

you can see that you can have round off errors, therefore 0-999,999 is probably better, and so on for that too.

you got an answer from a non-mathemetician, let's see if that gets criticized!
the next time Dame Fortune toys with your heart, your soul and your wallet, raise your glass and praise her thus: “Thanks for nothing, you cold-hearted, evil, damnable, nefarious, low-life, malicious monster from Hell!”   She is, after all, stone deaf. ... Arnold Snyder
wrragsdale
wrragsdale
  • Threads: 4
  • Posts: 25
Joined: Mar 7, 2011
March 9th, 2011 at 11:39:46 AM permalink
But wouldn't it be fair to say that a RNG has no effect on dice probabilities because it's only choosing numbers at random. It has no knowledge that on six-sided dice there are more combinations to arrive at 7 than there are combinations to arrive at...let's say 2? More combinations to arrive at a 5 than at 12 etc...
Is my thinking flawed?
odiousgambit
odiousgambit
  • Threads: 327
  • Posts: 9734
Joined: Nov 9, 2009
March 9th, 2011 at 11:46:40 AM permalink
Quote: wrragsdale

But wouldn't it be fair to say that a RNG has no effect on dice probabilities because it's only choosing numbers at random. It has no knowledge that on six-sided dice there are more combinations to arrive at 7 than there are combinations to arrive at...let's say 2? More combinations to arrive at a 5 than at 12 etc...
Is my thinking flawed?




the dice don't know either, there is an equal chance that 1-6 comes up on one die, then on the second die that die thoughtlessly deals out 1-6 equally as well. It's just that there are 6 ways to get a 7, one way to get boxcars, etc
the next time Dame Fortune toys with your heart, your soul and your wallet, raise your glass and praise her thus: “Thanks for nothing, you cold-hearted, evil, damnable, nefarious, low-life, malicious monster from Hell!”   She is, after all, stone deaf. ... Arnold Snyder
DJTeddyBear
DJTeddyBear
  • Threads: 210
  • Posts: 11060
Joined: Nov 2, 2009
March 9th, 2011 at 12:25:55 PM permalink
Quote: wrragsdale

But wouldn't it be fair to say that a RNG has no effect on dice probabilities because it's only choosing numbers at random. It has no knowledge that on six-sided dice there are more combinations to arrive at 7 than there are combinations to arrive at...let's say 2? More combinations to arrive at a 5 than at 12 etc...
Is my thinking flawed?

Your thinking is flawed if you think the RNG picks ONE number from 2 to 12. It actually picks a number from 1 to 6. The program that is using the RNG does that twice, then adds the two random numbers to come up with the two dice total. It also compares them for Hard Ways, etc.
I invented a few casino games. Info: http://www.DaveMillerGaming.com/ ————————————————————————————————————— Superstitions are silly, childish, irrational rituals, born out of fear of the unknown. But how much does it cost to knock on wood? 😁
s2dbaker
s2dbaker
  • Threads: 51
  • Posts: 3259
Joined: Jun 10, 2010
March 9th, 2011 at 1:38:57 PM permalink
Quote: wrragsdale

But wouldn't it be fair to say that a RNG has no effect on dice probabilities because it's only choosing numbers at random. It has no knowledge that on six-sided dice there are more combinations to arrive at 7 than there are combinations to arrive at...let's say 2? More combinations to arrive at a 5 than at 12 etc...
Is my thinking flawed?

The RNG that I use simply delivers a number from zero up to but not including one with precision to 15 decimal places. It's up to me, as the programmer, to make that random number do what I want. In your example of a coin flip, I would take the number issued by the RNG and multiply it by 2. I would then truncate the remaining decimals leaving me with either a zero or a one with equal probability. I would then assign the word "heads" to zero and "tails" to the numner one.

In the case of the dice roll, you are talking about two seperate events with equal chances of being an integer from one to six. So it's up to me, the programmer, to grab two numbers from the RNG and assign them to two different variables from one to six. I then add up the two variables to properly simulate what happens in reality.

For a deck of cards, it gets complicated so what I do is let the RNG assign a value from zero up to but not including one to each "card" in my virtual deck. I then sort the cards by the number assigned and turn those numbers into Integers from one to 52 ( or 53 with a joker ) because integers are conceptually easier to work with. In the case of a tie, the database decides the order based on the primary key so the Two of Clubs will always come before the Ace of Spades. But with granularity into the trillions, that's a risk I'm willing to accept.
Someday, joor goin' to see the name of Googie Gomez in lights and joor goin' to say to joorself, "Was that her?" and then joor goin' to answer to joorself, "That was her!" But you know somethin' mister? I was always her yuss nobody knows it! - Googie Gomez
kp
kp
  • Threads: 7
  • Posts: 422
Joined: Feb 28, 2011
March 9th, 2011 at 3:21:27 PM permalink
Quote: s2dbaker

I then sort the cards by the number assigned and turn those numbers into Integers from one to 52 ( or 53 with a joker ) because integers are conceptually easier to work with. In the case of a tie, the database decides the order based on the primary key so the Two of Clubs will always come before the Ace of Spades.

Is the tie breaking done before or after the conversion to integers?
s2dbaker
s2dbaker
  • Threads: 51
  • Posts: 3259
Joined: Jun 10, 2010
March 9th, 2011 at 4:01:18 PM permalink
Quote: kp

Is the tie breaking done before or after the conversion to integers?

During. Here's the geekery:
Update	shuffle
Set sortorder = x.sortorder
From ( Select shuffle_num,
c_index,
row_number() over(order by sortorder) sortorder
From shuffle
Where shuffle_num = @l_shuffle_num ) x
Inner Join shuffle y
On x.shuffle_num = y.shuffle_num
And x.c_index = y.c_index
Shuffle is a table with three columns named shuffle_num, c_index and sortorder. The row_number() over(order by XXX) function returns an integer based on the row in which the shuffle_num and c_index are returned. I simply replace the sortorder with the row_number(). With this technique, I was able to increase the speed of the shuffler from 40 hands per second to 250 hands per second. Previously, I had been assigning an integer then looking to see if that integer was already assigned then generating another. It was kind of messy. Now it r0xXx0rZ!!
Someday, joor goin' to see the name of Googie Gomez in lights and joor goin' to say to joorself, "Was that her?" and then joor goin' to answer to joorself, "That was her!" But you know somethin' mister? I was always her yuss nobody knows it! - Googie Gomez
DJTeddyBear
DJTeddyBear
  • Threads: 210
  • Posts: 11060
Joined: Nov 2, 2009
March 9th, 2011 at 4:12:23 PM permalink
Quote: s2dbaker

Here's the geekery:

Out of curiosity, what language is that?
I invented a few casino games. Info: http://www.DaveMillerGaming.com/ ————————————————————————————————————— Superstitions are silly, childish, irrational rituals, born out of fear of the unknown. But how much does it cost to knock on wood? 😁
MathExtremist
MathExtremist
  • Threads: 88
  • Posts: 6526
Joined: Aug 31, 2010
March 9th, 2011 at 4:15:15 PM permalink
Quote: s2dbaker

For a deck of cards, it gets complicated so what I do is let the RNG assign a value from zero up to but not including one to each "card" in my virtual deck. I then sort the cards by the number assigned and turn those numbers into Integers from one to 52 ( or 53 with a joker ) because integers are conceptually easier to work with. In the case of a tie, the database decides the order based on the primary key so the Two of Clubs will always come before the Ace of Spades. But with granularity into the trillions, that's a risk I'm willing to accept.



Why are you using integer conversions for the RNG values? With this shuffle algorithm, I don't think you need to convert at all. If you have a table with two columns, A for cards and B for floats, then you just randomly assign the float values in column B, sort on it, and A is now your shuffled deck. On initialization you need to assign 0..51 to column A, but only once (not each time). The only thing you need to do for a new shuffle is re-enter floats for column B and re-sort. Then just use a DB cursor to deal the cards.
"In my own case, when it seemed to me after a long illness that death was close at hand, I found no little solace in playing constantly at dice." -- Girolamo Cardano, 1563
s2dbaker
s2dbaker
  • Threads: 51
  • Posts: 3259
Joined: Jun 10, 2010
March 9th, 2011 at 4:24:18 PM permalink
Quote: DJTeddyBear

Out of curiosity, what language is that?

Microsoft T-SQL
Quote: MathExtremist

Why are you using integer conversions for the RNG values? With this shuffle algorithm, I don't think you need to convert at all. If you have a table with two columns, A for cards and B for floats, then you just randomly assign the float values in column B, sort on it, and A is now your shuffled deck. On initialization you need to assign 0..51 to column A, but only once (not each time). The only thing you need to do for a new shuffle is re-enter floats for column B and re-sort. Then just use a DB cursor to deal the cards.

Notice too that I have a shuffle number. I can debug if I choose to save the shuffles which comes in handy early in the process. Also, I can have three processors hitting the same table and get 750 hands per second. If you know Microsoft T-SQL, you know that cursors = slow. Avoid at all costs! Once I have the order converted to Integers, I can select directly from the "deck" the card in the 23rd position should I choose to. It leaves me with lots of flexibility.
Someday, joor goin' to see the name of Googie Gomez in lights and joor goin' to say to joorself, "Was that her?" and then joor goin' to answer to joorself, "That was her!" But you know somethin' mister? I was always her yuss nobody knows it! - Googie Gomez
MathExtremist
MathExtremist
  • Threads: 88
  • Posts: 6526
Joined: Aug 31, 2010
March 9th, 2011 at 7:50:32 PM permalink
I don't know T-SQL specifically; my last real DB work was with Oracle over 7 years ago, but I wasn't ever working iteratively down a table like that. You might want to reconsider doing all this in SQL though. I'm running on a laptop with a low-voltage dual-core 1.3GHz, and I can do about 200,000 shuffles per second using C# code (including the Mersenne Twister as my RNG) and the free MSFT IDE.
"In my own case, when it seemed to me after a long illness that death was close at hand, I found no little solace in playing constantly at dice." -- Girolamo Cardano, 1563
s2dbaker
s2dbaker
  • Threads: 51
  • Posts: 3259
Joined: Jun 10, 2010
March 9th, 2011 at 8:44:57 PM permalink
Nice performance! I could really scramble up some data in a hurry with that. I'll look into it.
Someday, joor goin' to see the name of Googie Gomez in lights and joor goin' to say to joorself, "Was that her?" and then joor goin' to answer to joorself, "That was her!" But you know somethin' mister? I was always her yuss nobody knows it! - Googie Gomez
kp
kp
  • Threads: 7
  • Posts: 422
Joined: Feb 28, 2011
March 10th, 2011 at 9:25:24 AM permalink
Quote: MathExtremist

200,000 shuffles per second using C#



That's what I'm thinking. SQL is too slow for this type of thing.

But my original quandary was satisfied by using the floats for the sort and then using the rowid for the integer conversion.
s2dbaker
s2dbaker
  • Threads: 51
  • Posts: 3259
Joined: Jun 10, 2010
March 10th, 2011 at 10:21:21 AM permalink
Wow, no love here for the the SQL! I find it to be very flexible. I can make changes to stored procs while processes that use them are running and do a whole lot of programming no-nos in the interest of reduced development time. It's worth the trade-off just so that I don't have to compile :)
Someday, joor goin' to see the name of Googie Gomez in lights and joor goin' to say to joorself, "Was that her?" and then joor goin' to answer to joorself, "That was her!" But you know somethin' mister? I was always her yuss nobody knows it! - Googie Gomez
kp
kp
  • Threads: 7
  • Posts: 422
Joined: Feb 28, 2011
March 10th, 2011 at 10:31:59 AM permalink
Don't get me wrong. I love SQL and use it for tasks when I need to manage a lot of data. But you can't beat the speed of in memory processing and low level languages for optimizing tasks with massive repetition.
MathExtremist
MathExtremist
  • Threads: 88
  • Posts: 6526
Joined: Aug 31, 2010
March 10th, 2011 at 2:32:18 PM permalink
Quote: s2dbaker

Wow, no love here for the the SQL! I find it to be very flexible. I can make changes to stored procs while processes that use them are running and do a whole lot of programming no-nos in the interest of reduced development time. It's worth the trade-off just so that I don't have to compile :)



What, are you compiling by hand? :)

Seriously, my code is maybe 500 lines long - compiling takes less than a second. Also, the 32-bit version of the Microsoft debugger has an edit-and-continue function where you can change running code without recompiling. Either way, I think you'll find that with a 200x improvement in runtime, it's faster to stop, edit, recompile, and re-run than it is to churn through a sim using random functions in SQL. And that's assuming the RNG built into your database is worth using for Monte Carlo analyses...
"In my own case, when it seemed to me after a long illness that death was close at hand, I found no little solace in playing constantly at dice." -- Girolamo Cardano, 1563
s2dbaker
s2dbaker
  • Threads: 51
  • Posts: 3259
Joined: Jun 10, 2010
March 10th, 2011 at 8:08:37 PM permalink
Quote: MathExtremist

Either way, I think you'll find that with a 200x improvement in runtime, it's faster to stop, edit, recompile, and re-run than it is to churn through a sim using random functions in SQL. And that's assuming the RNG built into your database is worth using for Monte Carlo analyses...

The SQL RNG seems to be just fine. I tweeked it a little. I dropped an index from the shuffle table, stopped deleting and just did updates and it's twice as fast as it was before. still not 200,000 per second but I'm making progress ;)
Someday, joor goin' to see the name of Googie Gomez in lights and joor goin' to say to joorself, "Was that her?" and then joor goin' to answer to joorself, "That was her!" But you know somethin' mister? I was always her yuss nobody knows it! - Googie Gomez
  • Jump to: