-1

I am new to programming and I got stuck with random number generation. I can simply generate random numbers using random function "randint" but could not generate set of random numbers. For instance i want to get 10 random numbers.

from random import randint
x = randint(1, 100)
y = randint(1, 100)
isFailedTest = (5<=x<=15) and (10<=y<=11)
selected_test = [x,y]
while (isFailedTest == False):

I can generate 1 random number at one time but not 10 at one time. Here 1 number mean 2 dimensional number example (x,y) = (10,20) I want to get 10 random numbers (x,y) after my while condition. How do I achieve that? I am very new to programming so could not figure out what could be done. All help/ suggestion/ recommendation is highly appreciated.Thank you.

usethedeathstar
  • 2,219
  • 1
  • 19
  • 30
sub
  • 51
  • 1
  • 6
  • As a Java programmer, I would simply write a for loop. There might be a more "pythonic" way to do this however. – Code-Apprentice Sep 08 '14 at 07:45
  • Use randint function 10 times to get 10 random numbers. In your code, you had used randint two times to get two random numbers for x and y. Yes, you can use randint as many time as you like. If you want to make array of random number, set random number into array one by one. – Fumu 7 Sep 08 '14 at 07:45

5 Answers5

6

Simple solution

array = [(randint(1, 100), randint(1, 100)) for i in range(10)]

Better solution

The following solution is more flexible and reusable.

from functools import partial
from random import randint


def randints(count, *randint_args):
    ri = partial(randint, *randint_args)
    return [(ri(), ri()) for _ in range(count)]


print(randints(10, 1, 100))
Peter Sutton
  • 1,145
  • 7
  • 20
ZomZom
  • 132
  • 5
3

Requirement - "Here 1 number mean 2 dimensional number example (x,y) = (10,20) I want to get 10 random numbers (x,y)"

>>> from random import randint as r
>>> array = [ (r(1,100), r(1,100)) for i in xrange(10)]
Sriram
  • 513
  • 3
  • 15
1

Why don't you just do:

from random import randint
randoms = []
for i in range(10):
    randoms.append((randint(1,100),randint(1,100)))

Then randoms will be an array of 10 integers, randomly generated between 1 and 100 (inclusive).

To be quite clear: what is going on here is that you make an empty list called randoms. Then the for loop executes ten times, each time appending a new tuple of two random integers to the list randoms.

Newb
  • 2,810
  • 3
  • 21
  • 35
  • Use list comprehensions instead of a loop. –  Sep 08 '14 at 07:46
  • 1
    @Tichodroma 1. Do you really think list comprehensions are appropriate in an answer for a total beginner? 2. I don't think there is a strong case for a list comprehension over a loop here. At a bytecode level, the two are nearly equivalent. A list comprehension is a *tiny* bit faster, and no more. See here for more: http://stackoverflow.com/questions/22108488/are-list-comprehensions-and-functional-functions-faster-than-for-loops – Newb Sep 08 '14 at 07:49
  • 1. Yes. Teach pythonic Python. 2. We don't talk about bytecode but about pythonic code. –  Sep 08 '14 at 09:53
  • @Tichodroma 1. What's the point in "teaching" pythonic Python if the asker doesn't even know how to populate a list? Just look at the OP's source code, and you know that a list comprehension will be utterly incomprehensible to them. (No offense to the OP intended.) 2. How can you dismiss the topic of bytecode-level efficiency? – Newb Sep 08 '14 at 14:10
1
from random import randint
r = []
N = 10
for x in range(N):
    a = randint(5,15)   # rand number between 5 and 15
    b = randint(10,11)  # rand number between 10 and 11
    r.append((a,b))

# r <-- contains N tuples with random numbers
Miguel Prz
  • 13,718
  • 29
  • 42
0

NumPy should do the job

np.random.randint(low=1, high=100, size=10)