# Making random number limit inclusive

Page 1 of 1

## 1 Replies - 573 Views - Last Post: 26 September 2012 - 10:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=293348&amp;s=e104e52a6f920c2e33b3ecbdeeaa9ce9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Hiatus

Reputation: 0
• Posts: 81
• Joined: 27-May 09

# Making random number limit inclusive

Posted 26 September 2012 - 08:39 PM

Hey all, im trying to get

``` x = random.uniform(0,1)
```

to include both 0 and 1 in the random number generator limits. Help?

Thanks!
Is This A Good Question/Topic? 0

## Replies To: Making random number limit inclusive

### #2 atraub

• Pythoneer

Reputation: 760
• Posts: 2,015
• Joined: 23-December 08

## Re: Making random number limit inclusive

Posted 26 September 2012 - 10:17 PM

well, it already will include the first value. The differences between [0,1) and [0,1] are infinitely small, and I wouldn't worry about it tooo much.

But I understand, you might REALLY want to have 1 included. In that case, you might just have to write your own inclusive_uniform form function.

EDIT:
I should show you this little experiment
```>>> longest=0
3
>>> for i in range(100000):
distance = len(str(random.uniform(0,1))[2:])
if distance > longest:
longest = distance

>>> longest
20

```

This experiment showed that there values with up to 20 digits after the decimal point returned from uniform. Here's my next experiment
```>>> smallest=20
>>> for i in range(200000):
distance = len(str(random.uniform(0,1))[2:])
if distance < smallest:
smallest = distance

>>> smallest
11
```
This experiment showed that the smallest values had 11 digits after the decimal place (I actually ran this one 100,000 times twice because I was amazed to get 11 and thought it might be a fluke). This tells us that we have at least 10 digits worth to play around with. If you did include 1, the odds of actually seeing it within your lifetime (under reasonable usage) are so astronomically small that it hardly seems worth putting it in there.

EDIT 2:
That experiment does make me question whether 0 is even included :-\

This post has been edited by atraub: 26 September 2012 - 10:28 PM