How to generate a random number with ease
One of the most important aspect of life and coding is generating something at random especially numeric data. if you want to create rolling dice project you will need to generate random number, there is no game app that is not using random logic. let me not waste time on this. To generate random number it not difficult at all just make use of any of the Method below
Method1
a) import random library and make use of random.random()
function to generate random number(floating) but Note: this will generate number between 0 and 1.
Example1
import random
randomNumber=random.random()
print(randomNumber)
Result>>
0.11209770967665211
0.2102655833597804
0.8074937463042062
0.22401585912043565
0.6454678067873696
Note: the example above will be generating a floating number(decimal number)
Example2
what if you you need a floating number beyond 1?? look at the following codes to get floating number between 0 and 100.
Codes>>>
import random
randomNumber=random.random()*100
print(randomNumber)
Result>>
56.19856830898462
11.276949771617096
29.23576419886832
38.667860543150745
82.81458817683554
Note: you can change 100 to whatever value you like. the result above is gotten after running the codes 5 times.
b) import random and implement random.randint() function to generate random integers at a particular range.
Example
let say we need to generate a random integer between 1 and 20. our codes will look like the following
Codes>>>
import random
randomNumber=random.randint(1,20)*100
print(randomNumber)
Result
3
16
12
8
6
The results above show the result we get after running the codes 5times. Note: the random.randint() function uses two parameters i. the start ii. the end as you can see in the example above that the first number is the starting point of our random number and the second is the end point.
Method2.
a) import all from random library by using 'asterik'and make use of random()
function to generate random number(floating) but Note: this will generate number between 0 and 1.
Example1
from random import *
randomNumber=random()
print(randomNumber)
Result>>
0.15120977096766521
0.2210265583359780
0.8707493746304206
0.24240158591204356
0.0645467806787369
Note: the example above is just like the one in method1 only that here we don't need 'random.' since we have import all with the asterik in the codes. also notice how we import the library in this example well.
Example2
what if you you need a floating number beyond 1?? the method is still the same except that 'random.' before the function in method1 is not needed anymore look at the example below. Codes>>>
from random import *
randomNumber=random.random()*100
print(randomNumber)
Result>>
56.19856830898462
1.027694771617096
29.23572414886832
48.667860567150745
62.81458811283554
Note:the above is gotten after running the codes 5times.
b) import random and implement random.randint()
function to generate random integers.
Example
let say we need to generate a random integer between 1 and 20. our codes will look like the following
Codes>>>
from random import *
randomInteger=randint(3,100)
print(randomInteger)
Result>>
34
78
91
23
52
that is different result gotten after running the codes 5 different times.
did you now understand how to get random number??
consider to share the article if you find it helpful. you can also chat me up on 09153036869
should you have any question or correction.