5

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.

Akash Mahapatra
  • 2,988
  • 1
  • 14
  • 28
  • 3
    You have a random number between 0 and 1. Can you figure out how to change that to 0 to 5? How about 0 to 5 → 30 to 35? 30 to 35 → integer 30 to 35? – Ry- Oct 17 '16 at 02:19
  • Sorry, i need to use "for" NOT "if". – Joel Oduor Bwana Oct 17 '16 at 02:33
  • `for _ in range(1): print(int(random.random() * 5) + 30)` [I don't see how `for` is relevant, but here's a way to force it in ;)] – anthony sottile Oct 17 '16 at 02:44
  • How to put these randomly generated numbers in a list though? I've tried list(int(random.random() * 5) + 30) but it doesn't work. – OGC Sep 20 '17 at 08:18
  • `[30 + random.random() * 5 for _ in range(10)]` – Martin Thoma Jun 03 '18 at 09:45
  • There is no need to loop in your assignment, I will revisit the requirements to understand better what is expected from the programming exercise. You could get your answer directly from the python API `import random random.seed(42) random.randint(30,35)` Even some of the solution that extent the domain from 0..1 to 30-35: `int(random.random() * 5) + 30` You could use a for loop to build an LCG that is a way of randomizing and not use the random function, instead implement your random generator. – Pegerto May 05 '21 at 18:53

6 Answers6

0

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

Noah Gift
  • 256
  • 1
  • 4
  • 9
0
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

Venantius
  • 2,471
  • 2
  • 28
  • 36
Utkarsh
  • 546
  • 1
  • 5
  • 14
0

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
naseefo
  • 720
  • 1
  • 9
  • 30
0
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)  
will-hedges
  • 1,254
  • 1
  • 9
  • 18
-1

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)
DubeyJI
  • 3
  • 3
  • Try to give some some description about your answer , refer https://stackoverflow.com/help/how-to-answer – Angel F Syrus Jun 01 '19 at 06:11
  • i am banned from answering does improving answer helps ? ( i have improved up to my best knowledge but mods have deleted them can you please help ) – DubeyJI Jul 08 '22 at 06:50
-2

Try this code:

import random
def function():
    random.seed(70)
    for i in range(0,10):
        print(5*random.random()+30)
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
navya
  • 1