Bovada is the only Internet casino endorsed by the Wizard.
Here are my reasons why and my promise of support.

Random Number Generators

Page 2 of 3<123>
March 9th, 2011 at 4:01:18 PM permalink
s2dbaker
Member since: Jun 10, 2010
Threads: 34
Posts: 1204
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!!
March 9th, 2011 at 4:12:23 PM permalink
DJTeddyBear
Member since: Nov 2, 2009
Threads: 105
Posts: 5682
Quote: s2dbaker
Here's the geekery:
Out of curiosity, what language is that?
Superstitions are silly, childish, irrational rituals, born out of fear of the unknown. But how much does it cost to knock on wood?
March 9th, 2011 at 4:15:15 PM permalink
MathExtremist
Member since: Aug 31, 2010
Threads: 45
Posts: 2511
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
March 9th, 2011 at 4:24:18 PM permalink
s2dbaker
Member since: Jun 10, 2010
Threads: 34
Posts: 1204
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.
March 9th, 2011 at 7:50:32 PM permalink
MathExtremist
Member since: Aug 31, 2010
Threads: 45
Posts: 2511
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
March 9th, 2011 at 8:44:57 PM permalink
s2dbaker
Member since: Jun 10, 2010
Threads: 34
Posts: 1204
Nice performance! I could really scramble up some data in a hurry with that. I'll look into it.
March 10th, 2011 at 9:25:24 AM permalink
kp
Member since: Feb 28, 2011
Threads: 7
Posts: 422
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.
March 10th, 2011 at 10:21:21 AM permalink
s2dbaker
Member since: Jun 10, 2010
Threads: 34
Posts: 1204
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 :)
March 10th, 2011 at 10:31:59 AM permalink
kp
Member since: Feb 28, 2011
Threads: 7
Posts: 422
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.
March 10th, 2011 at 2:32:18 PM permalink
MathExtremist
Member since: Aug 31, 2010
Threads: 45
Posts: 2511
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
Page 2 of 3<123>

 

Bovada is the only Internet casino endorsed by the Wizard.
Here are my reasons why and my promise of support.