Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialpeter keves
6,854 Pointswhat is the difference between randint and random choice?
what is the difference between randint and random choice?
2 Answers
David McCarty
5,214 PointsAs i understand it, random.randint(a, b) is used for getting a random number between two given numbers, a high and a low. E.g. if I want a number between 1 and 50 I would do random.randint(1, 50)
random.choice is used when you want to select an item at random in a given range or a list or something that is iterable. E.g. if you have a list object, say list_1 and it held a bunch of tuples or objects, or just random numbers, like this [5, 8, 9, 1, 5, 22, 54, 78, 55]. You could do random.choice(list_1) and it would return a random selection from within that list. Since there is no number 15 in list_1, random.choice(list_1) could not return a 15, it can only return something that is in the list.
Or, at least, that's how I understand it.
Hope that helps!
Ryan Ruscett
23,309 PointsHey,
To the iterpreter I go
>>> help(random.randint)
Help on method randint in module random:
randint(a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.
>>> help(random.random)
Help on built-in function random:
random(...) method of random.Random instance
random() -> x in the interval [0, 1).
OK I spoke to soon. This doesn't really help us does it. Check this out instead lol
random.random Return a random integer N such that a is less than or equal to N less than or equal to b.
random.randint Return the next random floating point number in the range [0.0, 1.0).
This is a bit more helpful
THE BIG REASON - is that random gives me a floating point number which requires me to do MATH YUCK!! for an integer. I mean I can get .845432 but that's not a number lol. Well it's a number but not a nice number like 8 or 1. I need to turn it into a number I can use and that is where MATH comes in. Where as randint just gives me back a number. Say betwen 1 and 10 I can get 2 or 1 or 6. That is the real reason these exist. For things like data science and where floating points and accuracy matters. You can use random but for games and whatever else. Use randint works fine.
Let me know if that helps!
Ryan Ruscett
23,309 Pointshaha totally misread that one lol. Random choice lol not random lol.
peter keves
6,854 Pointspeter keves
6,854 PointsCleared things up for me :)) thanks
David McCarty
5,214 PointsDavid McCarty
5,214 PointsGlad to help my friend :)