Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Truly random?

Reply
Thread Tools

Truly random?

 
 
Alvin
Guest
Posts: n/a
 
      12-06-2005
Well, I'm developing a Tetris game in SDL, but when it comes to
deciding the next block, I'm stuck. It's random, but when I try
something like seeding the randomizer with the time, it won't update as
fast as one block can fall, and the next to be determined. Generating
different numbers in one spur can work, but people can play Tetris for
hours (or even days), and so you can't predict how long. You could
constantly be making more with the same system as making, say 5 random
numbers out of a seed, but that would prove system intensive if the
game already uses a lot of memory (not that Tetris does, but I'm sure
there's a better way).

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=
Guest
Posts: n/a
 
      12-06-2005
"Alvin" <(E-Mail Removed)> writes:
> Well, I'm developing a Tetris game in SDL, but when it comes to
> deciding the next block, I'm stuck. It's random, but when I try
> something like seeding the randomizer with the time, it won't update as
> fast as one block can fall, and the next to be determined.


You only need to seed the PRNG once, at the start of the game.

DES
--
Dag-Erling Smørgrav - http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Randy Howard
Guest
Posts: n/a
 
      12-06-2005
Alvin wrote
(in article
<(E-Mail Removed) .com>):

> Well, I'm developing a Tetris game in SDL, but when it comes to
> deciding the next block, I'm stuck. It's random, but when I try
> something like seeding the randomizer with the time, it won't update as
> fast as one block can fall, and the next to be determined.


It sounds like you are trying to seed on every random call. You
only need to seed it once, or perhaps once per game worst case,
not every time. That would slow things down quite a bit, and
not achieve much at all.



--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      12-06-2005
If, as your subject line suggests, you want truly random numbers,
forget a PRNG like rand() or random(). The PR part stands for
PSEUDO-RANDOM. Computers aren't very good at generating truly
random numbers unless they are broken or they have specific hardware
for the purpose. Some attempts at truly random number generation
time keystrokes, or listen to thermal noise or radioactive decay,
or time events from outside the computer (like network traffic).

If you need truly random numbers for cryptography, a PRNG won't
do. The difference can get you killed.

>Well, I'm developing a Tetris game in SDL, but when it comes to
>deciding the next block, I'm stuck. It's random, but when I try
>something like seeding the randomizer with the time, it won't update as
>fast as one block can fall, and the next to be determined. Generating


Don't seed the PSEUDO random number generator more than once. Since
the time() call gives a time granularity of seconds on most (POSIX)
systems, and presumably blocks update faster than that, you'll get
sucky non-random numbers.

>different numbers in one spur can work, but people can play Tetris for
>hours (or even days), and so you can't predict how long. You could
>constantly be making more with the same system as making, say 5 random
>numbers out of a seed, but that would prove system intensive if the
>game already uses a lot of memory (not that Tetris does, but I'm sure
>there's a better way).


Seed once. Period. If calling rand() or random() alone slows down
the display too much, perhaps you need a faster CPU. But I doubt it.
All the graphics probably takes a lot more work than just generating
a pseudo-random number.

Gordon L. Burditt
 
Reply With Quote
 
Mogens Heller Jensen
Guest
Posts: n/a
 
      12-06-2005

"Alvin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Well, I'm developing a Tetris game in SDL, but when it comes to
> deciding the next block, I'm stuck. It's random, but when I try
> something like seeding the randomizer with the time, it won't update as
> fast as one block can fall, and the next to be determined. Generating
> different numbers in one spur can work, but people can play Tetris for
> hours (or even days), and so you can't predict how long. You could
> constantly be making more with the same system as making, say 5 random
> numbers out of a seed, but that would prove system intensive if the
> game already uses a lot of memory (not that Tetris does, but I'm sure
> there's a better way).
>


The ideal solution (which, however, requires an internet connection) is to
connect to www.random.org and download some random bytes...

They are truly random, since they come from *sampling backgound noise in the
atmosphere* (how cool is that?!).

This would also be a cool selling argument!

</funmaking>
<serious>
Check Randy Howard's answer )

-Mogens


 
Reply With Quote
 
Alvin
Guest
Posts: n/a
 
      12-06-2005
Well, it works like a charm for large numbers, but for small numbers
(say, 0 to 7), it often repeats them 3 or so times before changing. I
guess I could determine on how big the number is, so if it's > 100 and
< 200, select an right sided L block or something. Thanks very much,
though.

 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      12-06-2005
Gordon Burditt wrote
(in article <(E-Mail Removed)>):

> If, as your subject line suggests, you want truly random numbers,
> forget a PRNG like rand() or random(). The PR part stands for
> PSEUDO-RANDOM. Computers aren't very good at generating truly
> random numbers unless they are broken or they have specific hardware
> for the purpose. Some attempts at truly random number generation
> time keystrokes, or listen to thermal noise or radioactive decay,
> or time events from outside the computer (like network traffic).
>
> If you need truly random numbers for cryptography, a PRNG won't
> do. The difference can get you killed.


He said he's writing a Tetris game. I find it highly unlikely
he needs crypto random numbers for that.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      12-06-2005
Alvin wrote
(in article
<(E-Mail Removed). com>):

> Well, it works like a charm for large numbers, but for small numbers
> (say, 0 to 7), it often repeats them 3 or so times before changing.


Random does not mean "no repeats", especially in such a narrow
range. People often think that the output from a random number
generator should be evenly disributed. Bzzt.

If you are still reseeding it over and over, that will skew your
results. Fix that first.

If you think the random implementation on your platform is
flawed (after seeding it properly), then check out the Mersenne
Twister PRNG. It's probably overkill for a video game, but it
might make you feel better anyway.



--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      12-06-2005
>> If, as your subject line suggests, you want truly random numbers,
>> forget a PRNG like rand() or random(). The PR part stands for
>> PSEUDO-RANDOM. Computers aren't very good at generating truly
>> random numbers unless they are broken or they have specific hardware
>> for the purpose. Some attempts at truly random number generation
>> time keystrokes, or listen to thermal noise or radioactive decay,
>> or time events from outside the computer (like network traffic).
>>
>> If you need truly random numbers for cryptography, a PRNG won't
>> do. The difference can get you killed.

>
>He said he's writing a Tetris game. I find it highly unlikely
>he needs crypto random numbers for that.


True, but he asked for truly random numbers. Perhaps he's betting
large amounts on the outcome (anyone for Tetris machines next to
the video poker machines and the slots?), enough to make organized
efforts to cheat worth the time and trouble. If you're building
gambling machines which will take bets of real money, you *DO* need
crypto-quality random numbers or you're going to go broke.

Gordon L. Burditt
 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      12-06-2005
Gordon Burditt wrote
(in article <(E-Mail Removed)>):

>> He said he's writing a Tetris game. I find it highly unlikely
>> he needs crypto random numbers for that.

>
> True, but he asked for truly random numbers. Perhaps he's betting


I'd rather bet on the outcome of the Rose Bowl...

> large amounts on the outcome (anyone for Tetris machines next to
> the video poker machines and the slots?), enough to make organized
> efforts to cheat worth the time and trouble.


Ok, but I don't see a market for "Tetris Gambling". Maybe there
is, prove me wrong.


--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A truly uninstantiable class? Ian Pilcher Java 19 10-16-2005 03:58 PM
Truly Sign out a user Chris Kettenbach ASP .Net 2 10-15-2005 03:13 AM
[OT] Truly an MCNGP? T-Bone Microsoft Certification 0 11-05-2004 02:43 PM
Prospect of MCSE truly frightening Julian Ford MCSE 14 04-05-2004 04:57 PM
Re: truly abstract (platform independent) pathnames Harald Hein Java 9 08-17-2003 01:01 PM



Advertisments