Randomly generate an even number - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Randomly generate an even number (/thread-17581.html) Pages:
1
2
|
RE: Randomly generate an even number - Truman - Apr-17-2019 for x in range(14): return random.randrange(10,22,2)It works. by the way, is there any difference between random.randrange and random.choice? RE: Randomly generate an even number - Skaperen - Apr-18-2019 i don't think the loop is needed: import random def stochasticNumber(): return random.randrange(10,22,2) or maybe: import random def stochasticNumber(*args) return random.randrange(*args)[note: the use of *args may be beyond the class assignment limitations] so, what is the value of this function beyond inserting it into existing code? it looks like module "random" has plenty of capability. if i needed a non-uniform distribution, i would look at documentation to see what it has. for example,bell-curve distributions are easy enough to do if they are not already in there. RE: Randomly generate an even number - SheeppOSU - Apr-18-2019 You should be able to do this right - import random def stochasticNumber(): while True: rn = random.randint(9, 21) if rn % 2 == 0: return rn break RE: Randomly generate an even number - Truman - Apr-18-2019 Sheepp, while True: is a nice solution, looks better than for loop.
RE: Randomly generate an even number - SheeppOSU - Apr-18-2019 Thx, I take pride in my programming, no one at school really cares. I mean why would they, I'm only in 7th grade, all people care about is how good you are at Fortnite RE: Randomly generate an even number - ichabod801 - Apr-18-2019 Also note that there is a 0.00206% chance that you don't get an even number in 14 tries, and end up returning None. RE: Randomly generate an even number - Truman - Apr-18-2019 I don't quite understand that. But it's definitely better to use the range that was given in problem description. RE: Randomly generate an even number - Skaperen - Apr-19-2019 there is no need for a loop or any of the code in the body of that loop to get an even random number in a desired range, uniformly distributed. random.randrange() will do all you need. why do anything else? |