I want to create 10,000 random numbers withing the range of 1 to 100.
How can I do with python?
I can use for loop and with each for loop I can use random.sample() or random.random() function.
Is there any other way, without using any for loop I can generate it with built-in function ?
For python 3.6:
import random
my_randoms = random.choices(range(1,100), k=10000)
print(my_randoms)
For older would still need to use loops:
my_randoms = [random.randrange(100) for x in range(10000)]
Related
I have a code in Visual Basic that generates a vector of random numbers for a given seed (456 in my case). I need to replicate that code in Python and I am thinking if it is possible to generate with Python the same vector of random numbers, that is, to select the same seed as in VBA.
Let me show an example:
In VBA I have the following code:
Function rnd_seed(seed)
Dim x(1 To 10) As Double
Rnd (-1)
Randomize seed
For i = 1 To 10
x(i) = Rnd
Next i
rnd_seed = x
End Function
With this function, I obtain the values (for seed 456):
0.014666617
0.462389946
0.098651111
0.189074159
0.107685387
0.219710588
0.967558324
0.409745097
0.213494837
0.848815441
In Python, I use the following code (as suggested by K-D-G):
seed = 456
import random
random.seed(seed)
arr=[]
for i in range(10):
arr.append(random.random())
With this Python code, I obtain the following values:
[0.7482025358782363,
0.9665873085424435,
0.4352093219057409,
0.7942997804992433,
0.6481497216250237,
0.6174050474978059,
0.8222710780743806,
0.7895737180242367,
0.8864808985728122,
0.3264489135810307]
I see that the values obtained for VBA and Python are different. My question is if it is possible to generate with Python the same random numbers that I have generated with VBA. Is there any way to map the VBA and Python seeds?
Many thanks in advance!
Something like this:
import random
random.seed(seed)
arr=[]
for i in range(n):
arr.append(random.random())
Note random.random() returns value between 0 and 1 if you want integers over a range use random.randint(start, stop)
I’ve been working on a school project and need to do a loop consisting of a couple random numbers but I need them to output a different number each time. The code I’ve been using for the random numbers is this.
import random
a=random.randint(1,9)
I’m new to coding and just starting getting into python for fun and have looked everywhere for how to complete this loop but I can’t find anything that works.I know this code does not include a loop and the loop I was using before was “while True” and “for i in range” Thanks
You have not created any loop yet. You're generating random integer only once.
In order to generate more of them you have to use something like a for loop.
If you're familiar with the concept of range then this is a simple example of generating x-number of random integers.
import random
x = 10
for i in range(0, x):
a = random.randint(1, 9)
print(a)
I am assuming you are doing something like this. Your loop needs to call the random.randint function on each iteration of the loop.
a = random.randint(1,9)
for i in range(5):
print(a)
What you should be doing is this
for i in range(5):
print(random.randint(1,9))
If you want to have 5 values between 1 - 9 without repetition you can use the sample function of the random module. This will select n values from a list and store them in a new list.
array = random.sample(range(1, 9), 5)
for elem in array:
print(elem)
But if you want to have a new random value between 1 and 9 every iteration and you dont care if a number is repeated I would go with the answers the others already gave you.
Random works with a seed, with the same seed the same output
Easiest way to achieve that you want, you will have a different seed each time you run your program
import random
from datetime import datetime
random.seed(datetime.now())
for _ in range(10):
a=random.randint(1,9)
how to make a number generator on loops and limit each repetition to a maximum of 100, so that the number of numbers generated is 100 numbers.
Example of the resulting number:
EE-02100500004848486527
EE-02100500004848486432
up to 100 numbers.
so here I want the last 4 numbers to be randomized
this is my code
for x in range(100):
print("EE-02100500004848486527")
You can use this:
import random
with open("your_file.txt", "w") as f:
for _ in range(100):
f.write(f"EE-0210050000484848{random.randint(0, 9999):04d}\n")
You can use numpy's random generator or the standard random library below an quick example with numpy
import numpy as np
for i in range(100):
print("EE-0210050000484848{:04d}".format(np.random.randint(0,9999)))
to use the standard random library simply use import random and then random.randint() instead of the numpy function
I am new to python. Anyone have an answer to my problem? I have already made the list 1000 random numbers between 0 and 1. Now I would like to do the mean and their variance.
for i in range(1000):
print(A=random.random())
first you need to create the array, not just print it.
You can do this through numpy.
import numpy as np
Create random array of 1000 numbers between 0 and 1
a = np.random.rand(1000)
print(a.mean())
print(a.var())
You need to actually append them to a list, since printing them is not making a list out of them:
import random
myList = []
for i in range(1000):
myList.append(random.random())
Then, you can just use the mean and variance functions inside the statistics module:
import random
import statistics
myList = []
for i in range(1000):
myList.append(random.random())
print("The mean is", statistics.mean(myList))
print("The variance is", statistics.variance(myList))
Use the statistics module, it has variance function.
Also, your code will not generate a list. Use either a list comprehension or a loop appending to a list to generate your list of numbers.
# Importing Statistics module
import statistics
import random as r
# Creating a sample of data
sample = []
for _ in range(1000):
sample.append(r.random())
#Alternate method using list comprehension
sample = [r.random() for _ in range(1000)]
# Prints variance of the sample set
# Function will automatically calculate
# it's mean and set it as xbar
print("Variance of sample set is {statistics.variance(sample)})"
Source
In my code I am attempting to generate 8 random numbers using a for loop. I then add these to the end of my array of the name 'numbers'. Now I will like to add the numbers in this array together but I can't figure out a way to do this.
Below you will see my code.
def get_key():
numbers = []
for i in range(8):
i = random.randrange(33, 126)
numbers.append(i)
get_key()
You want to use sum
a = [1,2,3,4,5]
sum(a) # outputs 15
add as in sum? simply do sum(numbers).
As others have noted, you can use sum to iterate and accumulate over the list (the default accumulator for sum is int(), i.e. 0). Also, if this is the only purpose for the list, you can save memory by using a generator.
import random
get_key = lambda: sum(random.randrange(33, 126) for _ in range(8))
print( get_key() ) # 612
The real question is why are you trying to do this? It seems like there will be a more direct method by using a higher-level distribution. For example, the sum of n I.I.D. variables will approach a Normal distribution.