By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

Creating a more random int?

P: n/a
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 ...?
random.randint(3, 8) 7 random.randint(3, 8) 3 random.randint(3, 8) 3 random.randint(3, 8) 4 random.randint(3, 8) 3 random.randint(3, 8) 4 random.randint(3, 8) 7 random.randint(3, 8) 7 random.randint(3, 8) 7 random.randint(3, 8) 5 random.randint(3, 8) 6 random.randint(3, 8) 3 random.randint(3, 8) 8 random.randint(3, 8) 8 random.randint(3, 8) 5 random.randint(3, 8) 3 random.randint(3, 8)

4

Regards,
Steven

Jan 24 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
"Steven Macintyre" <st****@steven.macintyre.name> writes:
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.
Jan 24 '06 #2

P: n/a

Steven Macintyre wrote:
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"?
random.randint(3, 8) 7 random.randint(3, 8) 3 random.randint(3, 8) 3 random.randint(3, 8) 4 random.randint(3, 8) 3 random.randint(3, 8) 4 random.randint(3, 8) 7 random.randint(3, 8) 7 random.randint(3, 8) 7 random.randint(3, 8) 5 random.randint(3, 8) 6 random.randint(3, 8) 3 random.randint(3, 8) 8 random.randint(3, 8) 8 random.randint(3, 8) 5 random.randint(3, 8) 3 random.randint(3, 8)

4

Regards,
Steven


Jan 24 '06 #3

P: n/a
On Tue, 24 Jan 2006 18:47:01 +0200, Steven Macintyre wrote:
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.
--
Steven.

Jan 24 '06 #4

P: n/a
On Tue, 24 Jan 2006 09:36:49 -0800, Paul Rubin wrote:
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.
--
Steven.

Jan 24 '06 #5

P: n/a
Steven D'Aprano wrote:
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"...
Jan 25 '06 #6

P: n/a
On 2006-01-25, Magnus Lycka <ly***@carmen.se> wrote:
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.

--
Grant

Jan 25 '06 #7

P: n/a
On Wed, 25 Jan 2006 15:21:43 -0000 in comp.lang.python, Grant Edwards
<gr****@visi.com> wrote:
On 2006-01-25, Magnus Lycka <ly***@carmen.se> wrote:
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

--
Change is inevitable, progress is not.
Jan 25 '06 #8

P: n/a
Grant Edwards wrote:
[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>

Jan 25 '06 #9

P: n/a
Grant Edwards wrote:
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!
Jan 25 '06 #10

P: n/a
> 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

Jan 25 '06 #11

P: n/a
Well it IS unusual actually (~1/365.25). Please tell them for me.

-- Dave

Jan 25 '06 #12

P: n/a
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

Jan 25 '06 #13

P: n/a
On 2006-01-25, Fredrik Lundh <fr*****@pythonware.com> wrote:
Grant Edwards wrote:
[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. :)

--
Grant Edwards
gr****@visi.com
Jan 25 '06 #14

P: n/a
Grant Edwards wrote:
Grant Edwards wrote:
[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>

Jan 25 '06 #15

P: n/a
On 2006-01-25, Fredrik Lundh <fr*****@pythonware.com> wrote:
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...


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

--
Grant Edwards
gr****@visi.com

Jan 25 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.