TetWiis update – when is random not a random!

Answer – when its a rand() function call on the wii!

I was aware that there may be some frailties with the rand() function, but I didn’t expect to come across what I found!

My original code did the following to determine the next block

return (rand()%7) +1;

Which all seemed good and well.  I understood that it was only pseudo random, though what I didn’t expect was if I called it consecutively within quick succession of each other, that it would suddenly start returning the same number!  I noticed that if I dropped the blocks quick enough, that I could get 2 or 3 of the same type to appear in the list!  It took me a few goes to test this and make sure it wasn’t just chance, but sure enough, I could recreate it!

The solution – I decided to implement a mersenne twister random number generator!  I’d read about these types of generators before and searching the interweb, I came across http://www.bedaux.net/mtrand/ where sure enough, a simple enough implementation of it exists that I could pretty much take as a whole (Thanks Jasper!)

My randoms now really are random no matter how quickly I do it which obviously makes for a much better game of TetWiis 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *