-1

Hello I need some help in my programming course for python, I'm currently coding in python3.

I have menu based program where you can do various of things, I'm currently trying to create a option where the program will print out 10 numbers between 2 numbers that the user have already enterd using simple:

  • num1 = int(input("Enter your first number: "))
  • num2 = int(input("Enter your second number: "))

I need help witht the function that takes care of the randomizing part of the 10 numbers that will be printed out between 2 numbers that are already enterd.

kreqq_
  • 43
  • 1
  • 9

2 Answers2

0

Method 1 Here is a list of 10 elements in between num1 and num2

[random.randrange(*sorted([num1,num2])) for i in range(10)]

Why i used sorted

random.randrange only take increasing order of number

Here is the execution of code in IPython.

In [1]: import random
In [2]: num1 = int(input("Enter your first number: "))
Enter your first number: 10
In [3]: num2 = int(input("Enter your second number: "))
Enter your second number: 100
In [4]: for rand_num in [random.randrange(*sorted([num1,num2])) for i in range(10)]:
   .....:     print rand_num
   .....:     
69
63
14
22
98
39
66
21
26
72

Method 2

Here we use random.sample. If the range(num1,num2) have more than 10 population then only this method is valid.

random.sample(range(*sorted([num1,num2])),10)

Example of execution.

In [1]: random.sample(range(1,100),10)
Out[1]: [55, 5, 91, 19, 11, 42, 37, 45, 79, 86]
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
0
import random
num1, num2 = 10, 100
for i in range(0,10):
    print random.randrange(num1, num2)
Maorg
  • 83
  • 8