简体   繁体   中英

Output random number between 30-35 using Random.seed(), 'for' and multiplication in Python

I am new to programming. I had an assignment to write a code that will output a random number between 30 and 35. This code needs to use random.seed() , a for statement and a multiplication. I understand that the random.seed([x]) generates an initial value that could be used in the proceeding section of the code. However, I can't figure out how to proceed after obtaining the value of the random:

import random
random.seed(70)
print(random.random()) # This returns a value of 0.909769237923872

How do I use this value to generate a random value between 30 and 35?

Note: Without the specific directions above, I have been able to write two codes that function as desired, so please I am not looking for alternative ways of writing the code.

Here is another way to do it:

import random
#initial random seed based on current system time
#https://docs.python.org/2/library/random.html#random.seed

random.seed(9) #We set this so random is repeatable in testing
random_range = [30, 31, 32, 33, 34, 35]

while True:
     num = int(round(random.random(),1)*100)
     if num in random_range:
        print num
        break

The seed is set to 9, so if you run this over and over again you will get the same random value...thus setting a seed. Note that multiple iterations are run because it was mentioned to not use a choice like random.choice

import random

def problem():

    random.seed(70)
    list_no=[]
    i = 0
    while(i<10):
        x = (5*random.random()+30)
        list_no.append(x)
        i=i+1
    print(list_no)   

random.seed(70) is set to ensure that we generate the same random numbers.

list_no=[] is an empty list which is used to hold the values(output) in the form of a list.

random.random() generates value from 0-1 so by multiplying we can spread the random numbers out to cover the range 0 to 5. By adding you can shift these numbers up to the required range from 30 to 35, which is getting stored in x.

After storing the value we are appending the result to the list_no and incrementing i. After the loop gets executed we are printing the list

Here is a general code to find random number between any two floating point numbers.

from random import random, seed

def rand_no(x_lower, x_upper):
    '''
    Function to generate random number
    x_lower     :   lower bound value
    x_upper     :   upper bound value
    '''
    random_number = x_lower + (x_upper - x_lower)*random()
    return random_number

if __name__ == '__main__':

    seed_value = 70 # Seed value for random generator
    seed(seed_value)

    print('Printing a list of 10 random numbers\n')
    for i in range(10):
        sample_value = rand_no(30, 35)
        print("Number %d = %4.2f"%(i+1, sample_value))

Output is as follows:

Printing a list of 10 random numbers

Number 1 = 34.55
Number 2 = 31.47
Number 3 = 32.30
Number 4 = 30.68
Number 5 = 34.98
Number 6 = 30.77
Number 7 = 33.37
Number 8 = 32.99
Number 9 = 33.57
Number 10 = 32.05
import random

def random_no():
    # Making a list of 10 random reals numbers between 30 and 35
    random.seed(70)
    random_list=[]
    for i in range(10):
        x=(5*random.random()+30)
        random_list.append(x)
    print(random_list)  

random is package which is imported at the top

since we ( i and you ) can't implement logic for a random number generation ( or any other use case which is beyond our skill set at the moment ) we use packages like these

seed makes the same random number appear so the answer don't differ from the testcase

Try this way:

import random
def problem2_4():
    A = []
    random.seed(70)
    for i in range(0,10):
        A.append(5*random.random()+30)
        # it gives in a range of 0-1 so multiplied by 5
        # we need in range of 30-35 so we add it to 30
        # if 0.000000001 ~30.000000005 it is still in 30-35 range
        # if 0.999999901 ~34.999999505 it is still in 30-35 range
    # print the list of numbers generated at random
    print(A)

Try this code:

import random
def function():
    random.seed(70)
    for i in range(0,10):
        print(5*random.random()+30)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM