BBO random generator
#21
Posted 2012-October-11, 18:48
I thought one needed 96 bits of entropy to cover all 53,644,737,765,488,792,839,237,440,000 possibilities.
Carl
#22
Posted 2012-October-11, 18:59
#23
Posted 2012-October-11, 19:30
barmar, on 2012-October-11, 18:59, said:
The reason I mentioned this is because for years, Bridge Baron (and most other bridge playing programs) stated "4 billion random deals" etc... but about two years ago, Bridge Baron upgraded the internal dealer to cover all possible deals. I can't tell you why they did this, but it was implemented without any issues (I am a beta tester for them).
I would never "notice it" repeating (heck, I can replay my deals from a year ago and not notice it, and I did not mean to imply it was "broken". I was expressing curiosity, perhaps on the wrong thread?
Carl
#24
Posted 2012-October-11, 20:11
To tell the truth, I'm not sure how to calculate how many possible hands this algorithm can deal. If someone else knows, help me out. The period of rand() is about 2^35, but we also reseed it from time to time (whenever the BBO server is restarted). But this isn't the only thing we're calling rand() for -- it's also used in some movements.
#25
Posted 2012-October-12, 01:42
#26
Posted 2012-October-12, 17:26
barmar, on 2012-October-11, 20:11, said:
The general point I'm making is while there may be ways to improve it, what we've got is good enough for our needs and we have more important things to fix.
Gotcha. Didn't mean to stir things up, it's just anytime I can combine bridge and computer stuff, I enjoy it.
4 trillion boards ought to cover my needs.
Carl
#27
Posted 2012-October-13, 17:51
Quote
I thought one needed 96 bits of entropy to cover all 53,644,737,765,488,792,839,237,440,000 possibilities.
Without addressing BBO specifically...
Whether you can deal every possible deal is a separate question from how much entropy you have per deal. You can reach every possible deal completely deterministically just by enumerating them -- and if you did something like choosing the (30,000,000,000,000,000,000,000,000,001*n mod 53,644,737,765,488,792,839,237,440,000) th hand from the list to be your nth deal, it would be quite a long time before someone noticed the determinism.
There are many possible shuffling routines which can reach all possible deals, but can only "start from" 2^32 places as they work their way through the list.
There is a whole subdiscipline of "quasirandom number generators", guaranteed to eventually cover all possibilities and to scatter the first n points as uniformly around the sample space as possible. It's not much talked about now, but once upon a time it was used to do Monte Carlo integration faster and with tighter error bounds than by using pseudorandom numbers.
How many bits of entropy are used for each new hand addresses a separate question -- how well can the next deal be predicted, given the hands which have been dealt before it?
For humans, almost none are needed, if the pattern in which the hands are presented doesn't show any too-obvious trend. To defeat computer prediction of next boards, several bits per hand are desirable, but you can get away with quite a bit less than 96 per hand.
#28
Posted 2012-October-14, 16:04
If the RNG were just being used for dealing hands, and you saw a sequence of several hands, you might be able to calculate where it is in the random sequence, and predict the next set. But if hand dealing can alternate with an unpredictable number of other activities that also use the RNG, you lose your place. You might be able to estimate a range of other random activities that took place between deals, but that will give you a variety of possible deal sequences.
To get around this, you would need to somehow eavesdrop on ALL the activities that use the RNG, and know exactly how it's being used, to have a decent chance at predicting sequences.
#29
Posted 2012-October-14, 22:38
#30
Posted 2012-October-15, 11:49
http://sater.home.xs4all.nl/doc.html
There are two issues with the implementation barmar posted
1. c1=rand()%13; This will be biased towards certain values. A simple way of unbiasing it would be to modify the line to:
do {
c1=rand() & 0xF;
}
while (c1 >= 13);
2. Not sure what implementation of rand() the bbo compiler is using but AFAIK most implementations (e.g. GCC, VC) use a LCG with a period of 2^32, which is far below the minimum 2^96 that is required, meaning that it will only be able to deal a very small subset (1 in 2^64) of all the possible bridge hands. You should probably use something like a Mersenne Twister instead which has a period of 2^19937
Ideally, you would really want to implement something like that Big Deal has done.
1. Generate 512 bits of entropy, which can be reused forever
2. Start counter at 0
3. Hash(Entropy + Counter), I'd recommend using SHA256
4. Map the result to a bridge deal, e.g. using http://bridge.thomas...com/impossible/
5. Increment counter, go to 3 for next hand
#31
Posted 2012-October-15, 12:08
jandrew
#34
Posted 2012-October-16, 20:55
barmar, on 2012-October-15, 15:48, said:
That would be glibc, which uses a LCG with period of 2^31
Since you are on Linux, you have easy access to a strong PRNG in /dev/urandom and should probably just use that. Just add this and replace your calls to rand() with urand():
int urand() { static FILE* file = fopen("/dev/urandom", "r"); int random; fread(&random, sizeof(random), 1, file); return random; }
#35
Posted 2012-October-17, 12:53
It's the need to fix it that I'm not so sure of. I think folks are overstating the inadequacies of the current implementation.
#36
Posted 2012-October-17, 22:07
#37
Posted 2012-October-17, 23:37
Just remember that because of the birthday paradox, there will be a 50% probability of a deal being repeated after 2^15.5 = 46341 deals. Personally, I'm not comfortable with that, but the prerogative is yours.
#38
Posted 2012-October-18, 00:07
I can build a database containing all 2^31 possible hands that BBO can deal.
After seeing 13 cards (my own),
After seeing 26 cards (mine + dummy's), the exact distribution of the remaining 2 hands can almost always be determined. Occasionally there will be 2 possibilities but that will be very rare.
#39
Posted 2012-October-18, 00:26
#40
Posted 2012-October-18, 01:52
Antrax, on 2012-October-18, 00:26, said:
It doesn't matter. Because the LCG has 2^31 possible states when you start the generation, there can only be a maximum of 2^31 possible hands generated. Reseeding the LCG will only change the state from one of the 2^31 possible states to another of the 2^31 possible states. The maximum number of different hands that BBO can generate is 2^31, no matter how much entropy you try to introduce to it.
I actually underestimated the seriousness of the attack in my previous calculation:
There are 52c13 = 52!/(13! * 39!) = 6.35e11 possible 13 card hands
BBO can deal 2^31 = 2.15e9 possible hands
That means your 13 card starting hand is enough to determine the distribution of all the remaining 3 hands, 295 out of 296 times. (Not determine ~296 possible distributions as I previously miscalculated)
Of course, if rand() is called by another thread while we are in the middle of generating a hand, this will throw off the attack. I don't expect this to happen a lot though unless rand() is called by another thread very frequently. Also not certain if the other rand() calls happen in a different thread.