473,499 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Truly random?

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).

Dec 6 '05 #1
23 2252
"Alvin" <al***********@gmail.com> 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 - de*@des.no
Dec 6 '05 #2
Alvin wrote
(in article
<11**********************@f14g2000cwb.googlegroups .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

Dec 6 '05 #3
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
Dec 6 '05 #4
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.

Dec 6 '05 #5

"Alvin" <al***********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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 :o)

-Mogens
Dec 6 '05 #6
Gordon Burditt wrote
(in article <11*************@corp.supernews.com>):
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

Dec 6 '05 #7
Alvin wrote
(in article
<11*********************@g49g2000cwa.googlegroups. 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

Dec 6 '05 #8
>> 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
Dec 6 '05 #9
Gordon Burditt wrote
(in article <11*************@corp.supernews.com>):
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

Dec 6 '05 #10
In article <11*************@corp.supernews.com>,
Gordon Burditt <go***********@burditt.org> wrote:
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.


[OT]

My recollection is somewhat vague, as I just skimmed the material
some time ago, but if I recall what I have read correctly, the
professional casinos in places like Los Vegas do -not- use
crypto-quality random numbers, at least for some kinds of machines
such as slot machines. I seem to recall reading that they use a PRNG
but with algorithm parameters modified by a central computer.

The PNRG use has partly to do with managable auditing -- recording each
individual truly random number would take too much storage. The
ability to modify the parameters is used to adjust the payout rate
based upon the payin rate -- essentially once the intake has reached a
trigger value, the central computer adjusts the payout probabilities
upwards to make it more likely that someone will win a large prize. For
one thing, a slot machine that shows exactly the same losing line of
symbol 100 times in a row might be truly random, but it won't be
-perceived- to be random...

PNRG are losing propositions in gambling to the extent that someone
can effectively analyze the past history to predict the future outcomes.
A simple linear congruence PNRG is very risky in that respect, but
there are PRNG with large internal states, and if those states are
reseeded at intervals shorter than would be needed to extract useful
state information, then PRNG can become practical.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Dec 6 '05 #11
In article <11*********************@g49g2000cwa.googlegroups. com>,
Alvin <al***********@gmail.com> wrote:
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.


(Obviously, what follows is off-topic, not portable, and blah, blah, blah,
but anyway...)

Long ago and far away, using a compiler/development environment that need
not be mentioned here, I discovered that the higher order bits seemed to
be more random than the lower ordered ones. So, that the usual method (get
the number between, say, 0 and 32767, then divide it by the top of your
range, to get a random number between 0 and your range) tended to give less
good random results. So, I would take the result of random and bit shift
it left 5 (<< 5) to get the lower ordered bits more random. Seems to work.

Dec 6 '05 #12

Kenny McCormack wrote:
In article <11*********************@g49g2000cwa.googlegroups. com>,
Alvin <al***********@gmail.com> wrote:
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.


(Obviously, what follows is off-topic, not portable, and blah, blah, blah,
but anyway...)

Long ago and far away, using a compiler/development environment that need
not be mentioned here, I discovered that the higher order bits seemed to
be more random than the lower ordered ones. So, that the usual method (get
the number between, say, 0 and 32767, then divide it by the top of your
range, to get a random number between 0 and your range) tended to give less
good random results. So, I would take the result of random and bit shift
it left 5 (<< 5) to get the lower ordered bits more random. Seems to work.


Gee, I got a new asshole reamed for suggesting that very thing
a couple months ago despite it being in the FAQ.

Better sit on your helmet.

Dec 6 '05 #13
Kenny McCormack wrote:
In article <11*********************@g49g2000cwa.googlegroups. com>,
Alvin <al***********@gmail.com> wrote:
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.

(Obviously, what follows is off-topic, not portable, and blah, blah, blah,
but anyway...)

Long ago and far away, using a compiler/development environment that need
not be mentioned here, I discovered that the higher order bits seemed to
be more random than the lower ordered ones. So, that the usual method (get
the number between, say, 0 and 32767, then divide it by the top of your


You probably are talking about talking the remainder modulo the top
of the range. "Divide" was the other thingy, the one which worked
better than plain modulo.
range, to get a random number between 0 and your range) tended to give less
good random results. So, I would take the result of random and bit shift
it left 5 (<< 5) to get the lower ordered bits more random. Seems to work.


You probably mean the other left. The one most people call "right".
rand() << 5 certainly will give you five quite unrandom lower bits.

-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Dec 6 '05 #14
On 2005-12-06, Kenny McCormack <ga*****@yin.interaccess.com> wrote:
In article <11*********************@g49g2000cwa.googlegroups. com>,
Alvin <al***********@gmail.com> wrote:
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.


(Obviously, what follows is off-topic, not portable, and blah, blah, blah,
but anyway...)

Long ago and far away, using a compiler/development environment that
need not be mentioned here, I discovered that the higher order bits
seemed to be more random than the lower ordered ones. So, that the
usual method (get the number between, say, 0 and 32767, then divide it
by the top of your range, to get a random number between 0 and your
range) tended to give less good random results. So, I would take the
result of random and bit shift it left 5 (<< 5) to get the lower
ordered bits more random. Seems to work.


I assume you mean to the right.

And it is marginally on-topic, because the code of a PRNG with IIRC
characteristics like those you describe appears in the text of the C
standard.
Dec 6 '05 #15
Walter Roberson wrote:
PNRG are losing propositions in gambling to the extent that someone
can effectively analyze the past history to predict the future outcomes.
A simple linear congruence PNRG is very risky in that respect, but
there are PRNG with large internal states, and if those states are
reseeded at intervals shorter than would be needed to extract useful
state information, then PRNG can become practical.


Given a PRNG of the form

#include <stdint.h>

uint32_t foo(void)
{
static uint32_t s = /* unknown */;
const uint32_t n = /* unknown */;
const uint32_t m = /* unknown */;

s = s * n + m;
return s;
}

How many calls would it take to guess what the values of s, n and m were?

--
Simon.
Dec 6 '05 #16
Simon Biber said:
Given a PRNG of the form

#include <stdint.h>

uint32_t foo(void)
{
static uint32_t s = /* unknown */;
const uint32_t n = /* unknown */;
const uint32_t m = /* unknown */;

s = s * n + m;
return s;
}

How many calls would it take to guess what the values of s, n and m were?


None.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 7 '05 #17
In article <43***********************@news.optusnet.com.au> ,
Simon Biber <ne**@ralmin.cc> wrote:
Given a PRNG of the form #include <stdint.h> uint32_t foo(void)
{
static uint32_t s = /* unknown */;
const uint32_t n = /* unknown */;
const uint32_t m = /* unknown */;

s = s * n + m;
return s;
} How many calls would it take to guess what the values of s, n and m were?


As few as 3.

If you have 3 consequative values (x,y,z) that do not involve integer
overflow, then

s = (x*(y+z)-(x^2+y^2))/(z-y)
n = (z-y)/(y-x)
m = (y^2-x*z)/(y-x)

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Dec 7 '05 #18
Simon Biber wrote:
Given a PRNG of the form

#include <stdint.h>

uint32_t foo(void)
{
static uint32_t s = /* unknown */;
const uint32_t n = /* unknown */;
const uint32_t m = /* unknown */;

s = s * n + m;
return s;
}

How many calls would it take to guess what the values of s, n and m were?


Three calls plus a few seconds of compute time via brute force. There
may be a faster way.

Of course, if I can only see something like s/100000000, rather than s,
it will take many more samples.

--
Thad

Dec 7 '05 #19
Walter Roberson wrote:
In article <43***********************@news.optusnet.com.au> ,
Simon Biber <ne**@ralmin.cc> wrote:
Given a PRNG of the form
#include <stdint.h>


uint32_t foo(void)
{
static uint32_t s = /* unknown */;
const uint32_t n = /* unknown */;
const uint32_t m = /* unknown */;

s = s * n + m;
return s;
}


How many calls would it take to guess what the values of s, n and m were?

As few as 3.

If you have 3 consequative values (x,y,z) that do not involve integer
overflow, then


However, as is always the case for PRNGs, n can easily be chosen large
enough that there will always be integer overflow within three
consecutive values. It wouldn't produce anything resembling random
output if there wasn't integer overflow.
s = (x*(y+z)-(x^2+y^2))/(z-y)
n = (z-y)/(y-x)
m = (y^2-x*z)/(y-x)


Thanks for the formulas; they can be got by just typing

Solve[{x == s * n + m, y == x * n + m, z == y * n + m}, {s, n, m}]

into Mathematica. Unfortunately it doesn't work so well with

Solve[{x == Mod[s * n + m, 2^32],
y == Mod[x * n + m, 2^32],
z == Mod[y * n + m, 2^32]}, {s, n, m}]

--
Simon.
Dec 7 '05 #20
Mogens Heller Jensen wrote:
"Alvin" <al***********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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,
...


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?!).


Practically speaking, a reasonable PRNG would be more suitable. If you
needed secrecy (such as for high-stakes gambling), you wouldn't want to
connect to an external source with a non-secure connection, and even if
you secured the pipe, you would need reasonable assurances that the data
wasn't leaked (intentionally or otherwise) on the other end.

--
Thad

Dec 7 '05 #21
Jordan Abel <jm****@purdue.edu> wrote:

And it is marginally on-topic, because the code of a PRNG with IIRC
characteristics like those you describe appears in the text of the C
standard.


No, the code in the standard generates a result whose low-order bits are
just as random as the high-order bits (its internal state has 16 more
bits than its return values).

-Larry Jones

Why is it you always rip your pants on the day everyone has to
demonstrate a math problem at the chalkboard? -- Calvin
Dec 7 '05 #22
On 2005-12-07, la************@ugs.com <la************@ugs.com> wrote:
Jordan Abel <jm****@purdue.edu> wrote:

And it is marginally on-topic, because the code of a PRNG with IIRC
characteristics like those you describe appears in the text of the C
standard.
No, the code in the standard generates a result whose low-order bits are
just as random as the high-order bits (its internal state has 16 more
bits than its return values).


IIRC even though the shifting makes the low-order bits sufficiently
random to be useful, the higher ones are even more random - i.e. the
"randomness" [specifically, the length of the repeat period] is a
continuously increasing function as you go from lower to higher bits.

I don't know much about this, this is just how it was explained to me.
-Larry Jones

Why is it you always rip your pants on the day everyone has to
demonstrate a math problem at the chalkboard? -- Calvin

Dec 7 '05 #23
la************@ugs.com writes:
Jordan Abel <jm****@purdue.edu> wrote:

And it is marginally on-topic, because the code of a PRNG with IIRC
characteristics like those you describe appears in the text of the C
standard.


No, the code in the standard generates a result whose low-order bits are
just as random as the high-order bits (its internal state has 16 more
bits than its return values).


My tests[*] show the low-order bits to be significantly less
random than the high-order bits.
[*] Based on a version of the simplified poker test given in
Knuth volume 2.
Dec 28 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
3651
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
10
2481
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
10
5948
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
10
2484
by: Marshall Belew | last post by:
I'm trying to synchronize a network app that uses random numbers generated by System.Random. Rather than pass every randomly generated number, I just pass the seed. I'm seeing a result that leads...
41
2183
by: Mark R. Dawson | last post by:
I have never used a goto statement in my code, one of the first things I was told in my software classes a number of years ago was "goto statements are evil and lead to spagetti code - do not use...
15
2941
by: Steven Macintyre | last post by:
Hi all, I need to retrieve an integer from within a range ... this works ... below is my out puts ... it just does not seem so random ... Is there perhaps a suggestion out there to create a...
1
338
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
6
4791
by: Mike Langworthy | last post by:
i can not seem to get this code to work. someone please help using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
114
3788
by: Andy | last post by:
Dear Python dev community, I'm CTO at a small software company that makes music visualization software (you can check us out at www.soundspectrum.com). About two years ago we went with decision...
0
7128
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7006
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7385
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4917
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.