Using the random module:
>>> import random
>>> L = range(100)
>>> amount = 10
>>> [random.choice(L) for _ in range(amount)]
[31, 91, 52, 18, 92, 17, 70, 97, 17, 56]
Answer from TerryA on Stack Overflowhow to make an array of lists of random numbers
Big array with random numbers with python - Stack Overflow
python - How to create a random array in a certain range - Stack Overflow
python - Simple way to create matrix of random numbers - Stack Overflow
Hi there,
I am currently trying to speed up the slowest part of my computing project which is generating large sets of random numbers. This is a physics project and they are actually a specific distribution but effectively they are generated using a function that takes no arguments and returns a list of random numbers (large list >100000 elements).
The problem is I need to do this >10000 times and analyse each data set individually and independently. I was hoping that i could use np.vectorize(generatingfunc) and make an np.zeros(10000) and then act the functions on each element in the array to generate an array of the lists of random numbers and was hoping this could run in parallel. I haven't used the cupy lib as i couldn't really figure out how to use it for this specific problem but i do have a compatible gpu. I have tried searching for many variations of this problem but can't find anything. Any help is appreciated Thanks!
What you want is
[random.random() for _ in xrange(100000)]
From the random module documentation:
random.sample(population, k) Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.
so when calling random.sample(range(10), 100000) you're trying to extract 100000 unique elements in a sequence of length 10 which obviously can't work.
Note that
random.random()returns a floating value between [0 ; 1)random.randrange([start], stop[, step])returns a random element from the sequencerange([start], stop[, step])random.randint(a, b)returns an integer value in [a ; b]- when using
random.sample, the equalitylen(population) >= kmust hold
I think you're after something like this:
vet = [random.randint(1,10) for _ in range(100000)]
You can just do (thanks user2357112!)
[np.random.uniform(1.5, 12.4), np.random.uniform(0, 5), ...]
using numpy.random.uniform.
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])
You can drop the range(len()):
weights_h = [[random.random() for e in inputs[0]] for e in range(hiden_neurons)]
But really, you should probably use numpy.
In [9]: numpy.random.random((3, 3))
Out[9]:
array([[ 0.37052381, 0.03463207, 0.10669077],
[ 0.05862909, 0.8515325 , 0.79809676],
[ 0.43203632, 0.54633635, 0.09076408]])
Take a look at numpy.random.rand:
Docstring: rand(d0, d1, ..., dn)
Random values in a given shape.
Create an array of the given shape and propagate it with random samples from a uniform distribution over
[0, 1).
>>> import numpy as np
>>> np.random.rand(2,3)
array([[ 0.22568268, 0.0053246 , 0.41282024],
[ 0.68824936, 0.68086462, 0.6854153 ]])