Forum Discussion
You can use this function to generate random integers in the Min...Max range:
Function RandomNumber(Min, Max) Randomize RandomNumber = Int(Min + Rnd*(Max - Min + 1)) End Function
Usage:
number = RandomNumber(0, 100)
- Viji1238 years agoOccasional Contributor
Function RandomNumber(Min, Max) Randomize RandomNumber = Int(Min + Rnd*(Max - Min + 1)) End Function
I tried to implement this logic using python , its not working
I tried as below
def RandomNumber(Min, Max)
Randomize
RandomNumber = Int(Min + Rnd*(Max - Min + 1))Here am getting error in the second line ie. "Randomize" doesnt exist in python. Please advice
- baxatob8 years agoCommunity Hero
import random random.randint(min_, max_) #will return random integer between min_ and max_
In Python you should use a module random to operate random values.
- Colin_McCrae8 years agoCommunity Hero
Just to add to baxatob's reply re. Python random numbers ....
randint (his example) is inclusive. So will return a random integer from 0-9 (including 0 and 9). So, technically, "between" isn't quite accurate.
Also, you probably want to seed it. (Which is what the "randomize" statement in the VB version does.)
In Python, use "random.seed()" (in this form - with no value passed in - it will use current time as the seed) to avoid getting the same sequence of (pseudo) random numbers every time. See here: http://stackoverflow.com/questions/22639587/random-seed-what-does-it-do - if you want an explanation.
Related Content
Recent Discussions
- 3 days ago
- 3 days ago
- 7 days ago