Go Back   Science Forums
View Single Post
Old 03-23-2009   #42 (permalink)
CraigD's Avatar
CraigD
Creating


Location:
Silver Spring, MD, USA
 
CraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond reputeCraigD has a reputation beyond repute
 



Not Ranked  0 score     
Exclamation Not an endless loop

Quote:
Originally Posted by phillip1882 View Post
Code:
while z <100:
   y = random.randint(0,100)
   x = random.randint(0,y)
   z = random.randint(0,x)
Quote:
Originally Posted by alexander View Post
Actually that code will never halt, because while you may have a random number equal to 100, your algo will never return z that is larger then 100.
This code will finish on average after 101^3 = 1030301 iterations of the loop, because random.randint(0,100) can return integers including 0 and 100, and “100 < 100” evaluates to false.

Alexander, perhaps you’re confusing python’s random.randint with the random function of other languages, where RANDOM(100) typically returns no integer greater than 99? Or confused “<” for “<=”?


----------------
Moderator: Computers and Technology; Medical Science; Science Projects and Homework; Philosophy of Science; Physics and Mathematics; Environmental Studies
Reply With Quote
 
» Advertisement
» Current Poll
Who's the sexiest man alive? Johnny Depp or Robert Pattinson?
Johnny Depp - 27.27%
3 Votes
Robert Pattinson - 0%
0 Votes
Someone else (please specify) - 45.45%
5 Votes
I'm too macho to think a guy is sexy - 27.27%
3 Votes
Total Votes: 11
You may not vote on this poll.


All times are GMT -8. The time now is 05:02 AM.

Hypography?

Hypography [n.]: A combination of "hyperlink" and "bibliography" - ie, a list of links to electronic documents. Comparable to discography and bibliography, but not cartography.

We have been online since May 2000, and aim to be the best place to find and share science-related content of all kinds.

Share the love!

Please add more science to your life. Use our RSS feeds on your blog, your portal, or your favorite feedreader!


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright © 2000-2009 Hypography
Part of the Hypography - Science for Everyone Network