How do the functions like rand and Math.random work? How were they made? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

How do the functions like rand and Math.random work? How were they made?

8th Mar 2017, 9:08 AM
Prabhakar Dev
Prabhakar Dev - avatar
3 Answers
+ 14
They are library functions created by mostly third party programmers of such languages. This might sound funny but they are actually built upon real mathematical equations, for instance I do know of someone who once created his own random library making use of the time library, he did it by creating a function which uses the time in seconds at that particular moment to generate a random number I.e class Random1: def random_no_generator(u): t = list(str(u)) y = t[(len(t))-1] return(round((y * 300)/450)) @classmethod def integer(cls): import time l = time.time() return(random _no_generator(l) Note: This should be packaged into a Random1.py script. So if you imported Random1 class's script and used the Random1 class method it was going to generate a random whole integer for you. I.e import Random1 y = Random1.integer() print(y) # outputs = random integer I hope I made sense to you though Note this code was written in python.
8th Mar 2017, 9:52 AM
Josephine Smith
Josephine Smith - avatar
+ 10
random numbers generated using these such modules are actually pseudo-random numbers or false random numbers! They aren't actually random, as they work on with some definite parameters and do an out-tracking random operations and display the final output as a randomly generated number!
19th Mar 2017, 10:40 AM
Harshit Gupta
Harshit Gupta - avatar
0
I think random is based on computer/server internal clock to return a random number.
8th Mar 2017, 9:45 AM
Geoffrey L
Geoffrey L - avatar