8

Suppose I want to create a list or a numpy array of 5 elements like this:

array = [i, j, k, l, m] 

where:

  • i is in range 1.5 to 12.4
  • j is in range 0 to 5
  • k is in range 4 to 16
  • l is in range 3 to 5
  • m is in range 2.4 to 8.9.

This is an example to show that some ranges include fractions. What would be an easy way to do this?

Clement Attlee
  • 723
  • 3
  • 8
  • 16

3 Answers3

11

You can just do (thanks user2357112!)

[np.random.uniform(1.5, 12.4), np.random.uniform(0, 5), ...]

using numpy.random.uniform.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • 2
    This already exists; it's [`np.random.uniform`](http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.random.uniform.html). – user2357112 Apr 04 '16 at 20:19
  • [Apparently the middle 3 elements have to be integers.](http://stackoverflow.com/questions/36412006/how-to-create-a-random-array-in-a-certain-range?noredirect=1#comment60440012_36412006) – user2357112 Apr 04 '16 at 20:24
  • 1
    This is a good answer, but it ignores the fact that some of the ranges don't include fractions. – Clement Attlee Apr 04 '16 at 20:24
  • @ClementAttlee Thanks. Not sure what "some of the ranges don't include fractions" means, though. – Ami Tavory Apr 04 '16 at 20:27
  • @AmiTavory - You can use `np.random.randint` to create integer. See my answer: http://stackoverflow.com/questions/36412006/how-to-create-a-random-array-in-a-certain-range#answer-36412179 – MSeifert Apr 04 '16 at 20:29
  • @MSeifert Ah, got it. Good answer. – Ami Tavory Apr 04 '16 at 20:29
  • @AmiTavory `j`, `k`, and `l` should only produce random numbers that are natural (i.e no fractions). For the rest of the pack, it's ok for the randomly generated numbers to be fractional. Sorry if I am not clear. – Clement Attlee Apr 04 '16 at 20:29
  • 1
    @ClementAttlee Got it. I suggest you seeMSeifert's answer, then - he apparently addressed it (sorry to say, but I didn't get that from the question). – Ami Tavory Apr 04 '16 at 20:30
6

I would suggest generating them by hand and create the list later:

import numpy as np
i = np.random.uniform(1.5, 12.4)
j = np.random.randint(0, 5)  # 5 not included use (0, 6) if 5 should be possible
k = np.random.randint(4, 16) # dito
l = np.random.randint(3, 5)  # dito
m = np.random.uniform(2.4, 8.9.)

array = np.array([i, j, k, l, m]) # as numpy array
# array([  3.33114735,   3.        ,  14.        ,   4.        ,   4.80649945])

array = [i, j, k, l, m]           # or as list
# [3.33114735, 3, 14, 4, 4.80649945]

If you want to create them all in one go you can use np.random.random use the range and the lower-bound to modify them and convert them to integer where you don't want floats:

# Generate 5 random numbers between 0 and 1
rand_numbers = np.random.random(5) 

# Lower limit and the range of the values:
lowerlimit = np.array([1.5, 0, 4, 3, 2.4])
dynamicrange = np.array([12.4-1.5, 5-0, 16-4, 5-3, 8.9-2.4]) # upper limit - lower limit

# Apply the range
result = rand_numbers * dynamicrange + lowerlimit

# convert second, third and forth element to integer
result[1:4] = np.floor(result[1:4]) 

print(result)
# array([ 12.32799347,   1.        ,  13.        ,   4.        ,   7.19487119])
MSeifert
  • 145,886
  • 38
  • 333
  • 352
1
import random
array = [random.uniform(1.5, 12.4), random.uniform(0,5)]

print(array)

prints:

[9.444064187694842, 1.2256912728995506]

You might want to round these with round()

Keatinge
  • 4,330
  • 6
  • 25
  • 44