Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

On pseudorandom numbers and Tetris

Name: Anonymous 2009-11-15 11:24

So I'm programming a Tetris clone in Sepples and SDL. I need a method of generating a random number between 0 and 6 (seven different types of blocks) that gives a, more or less, even chance for each value to occur. I could use ctime and cstdlib to generate a pseudorandom number, then modulus that number by 7, but that wouldn't be an even chance. Some pieces would fall more often than others.

How does /prog/ get their pseudorandoms to be convincing?

Name: Anonymous 2009-11-16 2:25

andrey@andrey-linux:~$ perl -e '$a[int 7*rand]++ foreach 1..10_000_000; print join " ",@a[0..6],"\n"'
1429676 1429456 1426992 1429611 1428681 1425877 1429707
andrey@andrey-linux:~$ perl -e '$a[int(0x10000*rand)%7]++ foreach 1..10_000_000; print join " ",@a[0..6],"\n"'
1428773 1428597 1428502 1427044 1429391 1429428 1428265
andrey@andrey-linux:~$


Perl uses srand and rand. You can write similar program in C and see for yourself. Why did you even think that it wouldn't be an even chance?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List