4 ответов
+ 4
The Python course covers this in Functions & Modules → Modules chapter, with the following example:
import random
for i in range(5):
value = random.randint(1, 6)
print(value)
If you mean to generate random numbers without using tools (importing modules) you would probably be reinventing the wheel. Sorry if I misunderstood your point.
Hth, cmiiw
+ 2
Yes, you can... but I agree with Ipang about reinventing the wheel.
If have academic interest or just curious about it, Google "Linear Congruential Generator" and make your own! This is a good starting point.
This algorithm is simplest way to generate pseudo-random numbers in a defined interval, all of them equally possible as an outcome of the generator (they have a Uniform distribution).
Pay A LOT of attention to the role of the initial seed as this sets the beginning of the pseudo-random sequence of numbers you will get. Same seed means same "pseudo-random" sequence.
You will need some experimentation, patience and a bit of math background (probability), but it can be done.
Hope it helps.
0
thank you. my interest is both curiosity and academic.
I don't know where i got tools from. for some reason o thought the standard library was referred to as python tools.
i will look into the linear congruential generator. it is nice to have things like this provided by others but in general i like to learn how to such things as well.
my question would might be better worded... is it possible to generate random numbers from within the core code without importing from external modules.
0
wikipedia actually provides a sample random number generator function written in python code.
https://en.m.wikipedia.org/wiki/Linear_congruential_generator