Creating a more random int?

S

Steven Macintyre

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 more random int ...?
4

Regards,


Steven
 
P

Paul Rubin

Steven Macintyre said:
I need to retrieve an integer from within a range ... this works ... below
is my out puts ... it just does not seem so random ...

It's pretty normal for random small ints to not look random. Are
there some statistical tests that the prng is definitely failing?
Is there perhaps a suggestion out there to create a more random int ...?

Try reading bytes from os.urandom and converting them to ints in your
program.
 
M

mensanator

Steven said:
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 ...

What's wrong with it?
Is there perhaps a suggestion out there to create a more random int ...?

What do you think the output should be?

What's your criteria for "more random"?
 
S

Steven D'Aprano

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

You are choosing from six values. You should expect each value _about_
17% of the time, in the long-term. On your tiny sample of 17 samples, you
get this:

7 3 3 4 3 4 7 7 7 5 6 3 8 8 5 3 4

3 occurs five times or 29%
4 occurs three times or 18%
5 occurs two times or 12%
6 occurs one time or 6%
7 occurs four times or 23%
8 occurs two times or 12%

Looks pretty random to me.

With only 17 trials, it would be a major miracle to get near 17% for every
number. If you actually got something like this:

5 3 4 5 8 4 7 5 6 8 6 3 6 7 4 3 7

the odds are VERY strong that it is not really random. For one thing, it
is too good to be true. For another thing, there are no repeated values.
The chances of there being no repeated values is very small indeed.
Is there perhaps a suggestion out there to create a more random int ...?

23 is the most random int. Perhaps you should use that.

But all joking aside, the random number generator used by Python is one of
the best in the world. What is at fault is your intuition about what
random numbers should look like, not the random number generator.
 
S

Steven D'Aprano

Try reading bytes from os.urandom and converting them to ints in your
program.

He'll have the same problem with them, since the problem isn't the quality
of the randomness, but his intuition of what a random sequence should look
like.
 
M

Magnus Lycka

Steven said:
But all joking aside, the random number generator used by Python is one of
the best in the world. What is at fault is your intuition about what
random numbers should look like, not the random number generator.

This is a well known problem, and there are methods to detect
"too good to really be random" results in both research and
accounting, based on statistical analysis of the numbers.

The only thing the original poster really needs to understand
is that chance has no memory. If you roll a dice, and it's not
loaded, the chance that it will show a particular value is
always one in six. All values are as likely. It doesn't matter
that you've rolled 6 five times in a row. The change that the
dice will come up with a 6 a 6th time is is still 1/6. I.e.
it's as likely as any other particular value. Ten sixes in a
row is just as likely as any other sequence of numbers. The
chance for any sequence to occur is 6**-10.

Die rolls are not like drawing cards from a deck of cards, where
there are fewer queens left once we've drawn a queen.


/Magnus

P.S. Since I was a kid, I've heard people say: So you're born
on new years day--how unusual. Sigh... We humans have a very
good ability to spot things that seem to stick out from the
ordinary, such as dates that look particular, or the number
7 popping up thrice in a row. We also need to realize that,
while these things that stick out help us find significant
things, there are also a lot of "false positives"...
 
G

Grant Edwards

P.S. Since I was a kid, I've heard people say: So you're born
on new years day--how unusual.

Well, it happens to slightly less than 1/365th of the
population[1], so it is rather unusual. Of course it's no more
unusal that being born on June 19th or November 3rd or any
other date you choose...

[1] Assuming birth dates are uniformly distributed. Which the
probably aren't in countries where a significant portion of
the population schedules births to fit the hospital/doctors
schedule.
 
D

Dave Hansen

P.S. Since I was a kid, I've heard people say: So you're born
on new years day--how unusual.

Well, it happens to slightly less than 1/365th of the
population[1], so it is rather unusual. Of course it's no more
unusal that being born on June 19th or November 3rd or any
other date you choose...

[1] Assuming birth dates are uniformly distributed. Which the
probably aren't in countries where a significant portion of
the population schedules births to fit the hospital/doctors
schedule.

Google "birthday paradox" for a little OT fun.

Regards,
-=Dave
 
F

Fredrik Lundh

Grant said:
[1] Assuming birth dates are uniformly distributed. Which the
probably aren't in countries where a significant portion of
the population schedules births to fit the hospital/doctors
schedule.

or in countries that use "nominal birth dates" for people born in
poor parts of the country...

</F>
 
M

Magnus Lycka

Grant said:
Well, it happens to slightly less than 1/365th of the
population[1], so it is rather unusual. Of course it's no more
unusal that being born on June 19th or November 3rd or any
other date you choose...

Exactly. And I've never heard anyone say to my sons that it's
so unusual that they are born on Jan 30th or June 27th!
 
T

Tim Chase

Exactly. And I've never heard anyone say to my sons that it's
so unusual that they are born on Jan 30th or June 27th!

Now those born on Feb 29th...they're about a quarter as frequent :)

-tkc
 
D

davbrow

Steven,

Have you considered you might want a less random distribution? If you
just want to remove repeats so it 'seems' more random you could do that
(replace any repeat with another random value not equal to the last
value). It likely will not be as uniformly random as the original
sequence but it will be 'smoother' in some definition that you might
prefer for your uses. Be very careful though if you are going to use
it for technical work that requires a uniform distribution in the
formal sense.

-- Dave
 
G

Grant Edwards

Grant said:
[1] Assuming birth dates are uniformly distributed. Which the
probably aren't in countries where a significant portion of
the population schedules births to fit the hospital/doctors
schedule.

or in countries that use "nominal birth dates" for people born in
poor parts of the country...

FWIW, "nominal brith dates" is a phrase with exactly one hit in
google. :)
 
F

Fredrik Lundh

Grant said:
Grant said:
[1] Assuming birth dates are uniformly distributed. Which the
probably aren't in countries where a significant portion of
the population schedules births to fit the hospital/doctors
schedule.

or in countries that use "nominal birth dates" for people born in
poor parts of the country...

FWIW, "nominal brith dates" is a phrase with exactly one hit in
google. :)

try "nominal birth date", and you'll find lots of references to Saddam,
Jesus, and fish, and a few pages that actually explain the concept...

</F>
 
G

Grant Edwards

try "nominal birth date", and you'll find lots of references to Saddam,
Jesus, and fish, and a few pages that actually explain the concept...

I know. I was just impressed that you came up with a
single-hit phrase. It's harder than you might think. [And that
single hit did contain of an example of the concept -- with
clams, I believe].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,282
Messages
2,571,404
Members
48,096
Latest member
Kenkian2628

Latest Threads

Top