Is there any possible way to choose a random number in Python without using a module? So far I have:
import numpy as np
numbers = np.random.choice(range(5), 10000000, p=[0.1, 0.05, 0.1, 0.7, 0.05])
but that uses a module. Any advice?
Is there any possible way to choose a random number in Python without using a module? So far I have:
import numpy as np
numbers = np.random.choice(range(5), 10000000, p=[0.1, 0.05, 0.1, 0.7, 0.05])
but that uses a module. Any advice?
I would highly recommend using Python's built-in random number module, which comes stock with every install. I cannot think of a case where this would be a bad idea, unless you are dealing with some stripped down special build of Python. It will be faster than anything you can implement yourself in Python probably, and has been tested and used regularly. Testing your own random number generator is madness when you can use one that exists in a couple of lines.
# RANDOM NUMBER GENERATOR
the_set = set()
for i in range(10):
the_set.add(str(i))
for e in the_set:
print(int(e))
break
This does not use a module! So this works by creating a set(in which case the values are not ordered), then just prints the first value that it can find and the break is here to stop it from printing all the values of the set.
You reason why var i is added as a str is because if we were to add it as an int then the numbers would get ordered and the first or the smallest number would get printed.
Note that the number 10 will not generate, because it is until 10 and not including it! In order to print also 10, you would need too make the range range(11)
.
And 0 will also have a chance to get printed, if you don't want to 0 to appear just make the range range(1, 10)
.