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)
Related
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)
for x in range(16):
ran.append(random.randint(1, 500))
# print(ran)
Generating random numbers within a range is easy but how to generate random numbers from a given range that yields the same output in each execution for that particular range in python.
As long as you seed your random with the same seed, you'll get the same series of values:
random.seed(7) # Or any other arbitrary seed
for x in range(16):
ran.append(random.randint(1, 500))
In random number generators there is usually a seed() function that does that. If the argument to seed is constant, the generated sequence of numbers will be the same. So, just write random.seed(some_number) in the beginning of your code.
I am trying to generate random numbers that are used to generate a part of a world (I am working on world generation for a game). I could create these with something like [random.randint(0, 100) for n in range(1000)] to generate 1000 random numbers from 0 to 100, but I don't know how many numbers in a list I need. What I want is to be able to say something like random.nth_randint(0, 100, 5) which would generate the 5th random number from 0 to 100. (The same number every time as long as you use the same seed) How would I go about doing this? And if there is no way to do this, how else could I get the same behavior?
Python's random module produces deterministic pseudo random values.
In simpler words, it behaves as if it generated a list of predetermined values when a seed is provided (or when default seed is taken from OS), and those values will always be the same for a given seed.
Which is basically what we want here.
So to get nth random value you need to either remember its state for each generated value (probably just keeping track of the values would be less memory hungry) or you need to reset (reseed) the generator each time and produce N random numbers each time to get yours.
def randgen(a, b, n, seed=4):
# our default seed is random in itself as evidenced by https://xkcd.com/221/
random.seed(seed)
for i in range(n-1):
x = random.random()
return random.randint(a, b)
If I understood well your question you want every time the same n-th number. You may create a class where you keep track of the generated numbers (if you use the same seed).
The main idea is that, when you ask for then nth-number it will generate all the previous in order to be always the same for all the run of the program.
import random
class myRandom():
def __init__(self):
self.generated = []
#your instance of random.Random()
self.rand = random.Random(99)
def generate(self, nth):
if nth < len(self.generated) + 1:
return self.generated[nth - 1]
else:
for _ in range(len(self.generated), nth):
self.generated.append(self.rand.randint(1,100))
return self.generated[nth - 1]
r = myRandom()
print(r.generate(1))
print(r.generate(5))
print(r.generate(10))
Using a defaultdict, you can have a structure that generates a new number on the first access of each key.
from collections import defaultdict
from random import randint
random_numbers = defaultdict(lambda: randint(0, 100))
random_number[5] # 42
random_number[5] # 42
random_number[0] # 63
Numbers are thus lazily generated on access.
Since you are working on a game, it is likely you will then need to preserve random_numbers through interruptions of your program. You can use pickle to save your data.
import pickle
random_numbers[0] # 24
# Save the current state
with open('random', 'wb') as f:
pickle.dump(dict(random_numbers), f)
# Load the last saved state
with open('random', 'rb') as f:
opened_random_numbers = defaultdict(lambda: randint(0, 100), pickle.load(f))
opened_random_numbers[0] # 24
Numpy's new random BitGenerator interface provides a method advance(delta) some of the BitGenerator implementations (including the default BitGenerator used). This function allows you to seed and then advance to get the n-th random number.
From the docs:
Advance the underlying RNG as-if delta draws have occurred.
https://numpy.org/doc/stable/reference/random/bit_generators/generated/numpy.random.PCG64.advance.html#numpy.random.PCG64.advance
I am attempting to create nxn matrices which contain random variables between 0 and 1. I am created the following code (default of matrice size is 10x10):
class MatrixParameters:
def __init__(self, matrix_size=10):
self.matrix_size= matrix_size
def RMatrix(param):
assert isinstance(param, MatrixParameters)
rnd_1 = random.random(param.matrix_size)
rnd_2 = random.random(param.matrix_size)
return rnd_1,rnd_2
I am getting the error: random() takes no arguments (1 given). I have read the forum and I am not sure .choice is the way forward. How have I screwed up?
Error is kind of explicit, but you can also check the random.random() doc.
As you can see, random.random() does not expect any argument, but you given one (param.matrix_size).
If you wants a random integer between two integers, uses random.randint() like random.randint(0, params.matrix_size)
random method does not take arguments.
If you would like to generate range of numbers between 0 to 1. Then you could do,
[random.random() for i in range(10)] # To generate 10 random numbers, replace 10 with params.matrix_size.
Or with numpy.
import numpy as np
np.random.random(10)
I really need help as I am stuck at the begining of the code.
I am asked to create a function to investigate the exponential distribution on histogram. The function is x = −log(1−y)/λ. λ is a constant and I referred to that as lamdr in the code and simply gave it 10. I gave N (the number of random numbers) 10 and ran the code yet the results and the generated random numbers gave me totally different results; below you can find the code, I don't know what went wrong, hope you guys can help me!! (I use python 2)
import random
import math
N = raw_input('How many random numbers you request?: ')
N = int(N)
lamdr = raw_input('Enter a value:')
lamdr = int(lamdr)
def exprand(lamdr):
y = []
for i in range(N):
y.append(random.uniform(0,1))
return y
y = exprand(lamdr)
print 'Randomly generated numbers:', (y)
x = []
for w in y:
x.append((math.log((1 - w) / lamdr)) * -1)
print 'Results:', x
After viewing the code you provided, it looks like you have the pieces you need but you're not putting them together.
You were asked to write function exprand(lambdr) using the specified formula. Python already provides a function called random.expovariate(lambd) for generating exponentials, but what the heck, we can still make our own. Your formula requires a "random" value for y which has a uniform distribution between zero and one. The documentation for the random module tells us that random.random() will give us a uniform(0,1) distribution. So all we have to do is replace y in the formula with that function call, and we're in business:
def exprand(lambdr):
return -math.log(1.0 - random.random()) / lambdr
An historical note: Mathematically, if y has a uniform(0,1) distribution, then so does 1-y. Implementations of the algorithm dating back to the 1950's would often leverage this fact to simplify the calculation to -math.log(random.random()) / lambdr. Mathematically this gives distributionally correct results since P{X = c} = 0 for any continuous random variable X and constant c, but computationally it will blow up in Python for the 1 in 264 occurrence where you get a zero from random.random(). One historical basis for doing this was that when computers were many orders of magnitude slower than now, ditching the one additional arithmetic operation was considered worth the minuscule risk. Another was that Prime Modulus Multiplicative PRNGs, which were popular at the time, never yield a zero. These days it's primarily of historical interest, and an interesting example of where math and computing sometimes diverge.
Back to the problem at hand. Now you just have to call that function N times and store the results somewhere. Likely candidates to do so are loops or list comprehensions. Here's an example of the latter:
abuncha_exponentials = [exprand(0.2) for _ in range(5)]
That will create a list of 5 exponentials with λ=0.2. Replace 0.2 and 5 with suitable values provided by the user, and you're in business. Print the list, make a histogram, use it as input to something else...
Replacing exporand with expovariate in the list comprehension should produce equivalent results using Python's built-in exponential generator. That's the beauty of functions as an abstraction, once somebody writes them you can just use them to your heart's content.
Note that because of the use of randomness, this will give different results every time you run it unless you "seed" the random generator to the same value each time.
WHat #pjs wrote is true to a point. While statement mathematically, if y has a uniform(0,1) distribution, so does 1-y appears to be correct, proposal to replace code with -math.log(random.random()) / lambdr is just wrong. Why? Because Python random module provide U(0,1) in the range [0,1) (as mentioned here), thus making such replacement non-equivalent.
In more layman term, if your U(0,1) is actually generating numbers in the [0,1) range, then code
import random
def exprand(lambda):
return -math.log(1.0 - random.random()) / lambda
is correct, but code
import random
def exprand(lambda):
return -math.log(random.random()) / lambda
is wrong, it will sometimes generate NaN/exception, as log(0) will be called