~Answer the follopwing questions in comments in python code file plase~ Plase ex
ID: 3674252 • Letter: #
Question
~Answer the follopwing questions in comments in python code file plase~
Plase explain what you can, I am new to programming and find it dificult. I really appreciate it!
Python contains a random number generator that is implemented by first importing the random module and then calling the method randrange( ) to generate a random number.
Input the following code:
import random
print (random.randrange(5))
Execute the print statement several times. Put the print line in a loop to print a random value at least 20 times and record your results.
What range of numbers does the print statement produce? If you wanted to randomly generate the numbers from 1 to 5 what code would you have to add to the above print statement to do this? What would you change to get a random number in the range 1-100?
Explanation / Answer
import random;
while i<20
print(random.randrange(5));
i=i+1;
//this will print random 20 numbers upto 5 but not 5..
So output may be like:
0
1
2
4
0
0
3
2
1
2
1
2
4
3
2
0
1
2
4
3
If you wanted to randomly generate the numbers from 1 to 5:
import random
while(i<10)
print(random.randrange(1,6); // or you can also use print (random.randInt(1,5)); //random numbers from 1,2,3,4,5 will be displayed
i=i+1;
to get a random number in the range 1-100:
import random
while(i<20)
n=random.randrange(1,100); //it will include 1 but exclude 100
print(n);
i=i+1;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.