1 Replies - 535 Views - Last Post: 26 September 2012 - 10:17 PM Rate Topic: -----

#1 Hiatus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1