Numpy producing different random numbers despite seed [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I had a problem with NumPy producing different random numbers despite seeding.
code:
import numpy as np
import random
random.seed(1234)
vec = np.random.normal(0, 1, size=6)
print(vec)
As I had set the seed, I expected vec to contain the same random numbers in every run.
But what I got:
run: vec = [-0.80715758 0.8377004 -0.98711439 -0.23424405 -1.85368722 0.97827818]
run: vec = [-0.74409802 -0.85180205 0.84585489 -0.40506222 -1.31125093 -1.23055255]
run: vec = [ 2.21521007 1.38577035 0.25437804 0.84529466 -0.83334042 1.00452671]
I was just posting this question when I found the solution.
But to prevent others from doing the same mistake, I thought it might be a good idea to post it anyway.

My mistake was the following:
I set the seed of random, not of np.random!
When setting the seed of the NumPy random number generator, everything works as expected:
import numpy as np
np.random.seed(1234)
vec = np.random.normal(0, 1, size=6)
vec will always contain the same random numbers.

Related

quick sort algorithm by python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I tried to write some line of code for quick sort algorithm using numpy algorithm.
But It seems not working properly .
Can you help me solve it ?
import numpy as np
def quick_sort_np(narr):
"""A numpy version of quick sort algorithm"""
narr = np.array(narr)
if len(narr)<= 1 :
return narr
p = narr[-1] # I take the last item as pivot by convension
print (f"at this level the pivot is {p}")
lnarr = narr[narr<p]
print (f"----------------------> left arr is {lnarr}")
rnarr = narr[narr>p]
print (f"----------------------> right arr is {rnarr}")
return quick_sort_np(lnarr) + p + quick_sort_np(rnarr)
in case of [1,2,6,5,4,8,7,99,33] as input my code returns nothing and that's the question.
+ acting on np.arrays is element wise addition, not concatenation.

Python Undefined variable 'rg' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to run this code, but get an error "name 'rg' is not defined". Could you please tell which library I can import rg from?
import numpy as np
a = np.floor(10*rg.random((3,4)))
print(a)
print(a.shape)
import numpy as np
rg = np.random.default_rng(1)
a = np.floor(10*rg.random((3,4)))
print(a)
print(a.shape)
If you wanted the rg.random() to give a random integer between two numbers, you would use random.randint(num1, num2).
So in your case, you would use this:
import numpy as np
import random
a = np.floor(10*random.randint((3,4)))
print(a)
print(a.shape)
import numpy as np
a = np.floor(10*np.random.rand((3,4)))
print(a)
print(a.shape)
np is the alias you are assigning to numpy, not ng

(TypeError: 'float' object is not callable) when doing math [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am writing a code to calculate all of the energy levels for an electron as it jumps to different orbital levels in the Bohr model. I have some math written, but when I try to run it it says TypeError: 'float' object is not callable. What do I need to do?
Here is my code:
loopone = True
if (work == Element_symbols[0]):
energy = ((charge[0]*(0-13.6))/1)
print ('This is the energy level for the electron at the N=1 state.')
print (energy)
print ('These are all of the possible energies from the n=6 level')
nrgone = (-13.6((1/(5*5)-(1/(6*6)))))
nrgtwo = (-13.6((1/(4*4)-(1/(6*6)))))
nrgthree = (-13.6((1/(3*3)-(1/(6*6)))))
nrgfour = (-13.6((1/(2*2)-(1/(6*6)))))
nrgfive = (-13.6((1/(1*1)-(1/(6*6)))))
Energies = (nrgone, nrgtwo, nrgthree, nrgfour, nrgfive)
print (Energies)
loopone = False
I believe your problem is here
nrgone = (-13.6((1/(5*5)-(1/(6*6)))))
Don't you mean (notice the multiplication sign)?
nrgone = (-13.6*((1/(5*5)-(1/(6*6)))))

does python find errors where there are none? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
while plotting functions with heaviside function I came up with this piece of code, in Idle:
import numpy as np
import matplotlib.pyplot as plt
n_i = [-5, 5]
n = np.linspace(n_i[0], n_i[1], 1E3)
u1 [n+30>=0] = 1
u2 [n-15>=0] =1
u3 = u1 - u2
x = np.sin(2*np.pi*(n/15))*u3
plt.axis([-30,15,-5,5])
plt.xlabel('$n$',fontsize=20)
plt.ylabel('$x(n)$',fontsize=20)
plt.stem(n, x, "--k", linefmt='black', basefmt='black')
plt.grid()
plt.show()
and prior to today, it ran without errors, same with all other of my plots, I've been dealing with python for two years now and throughout classes it had the habit of finding errors where even teachers don't see them. am I missing something here? it says "u1 is not defined", but it is. I even compared with coworkers and classmates alike, haven't seen it put in any other way in the code for the plot. help!
You try to assign a value to u1[...] without even having created u1.
You should init u1 before trying to init its elements.

Plotting a complex function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a complex function to plot using Python, and then I have to find its root using ridder's method.
This is what I've done so far with my code but I keep running into a syntax error on a simple part of my code and have tried loads of things but nothing has given me a different result.
import math as m
from math import log
import numpy as np
import matplotlib.pyplot as plt
t = arange(0,250,0.1)
M = 2.8*10**6
n = 13.3*10**3
g = 9.8
u = 2510
plt.plot(u*(np.log((M)/(M-(n*t)))-g*t, t, 'r-^')
plt.xlabel('time')
plt.ylabel('velocity')
plt.show
Im being told my line where I am doing plt.xlabel has a syntax error and I can only assume the next line will do the same and I am not sure why.
Can someone point out my mistake please ?
first of all, arange belongs to numpy so you have to use np.arange
second, check parenthesis!
import math as m
from math import log
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0,250,0.1) #first line changed
M = 2.8*10**6
n = 13.3*10**3
g = 9.8
u = 2510
plt.plot(u*(np.log((M)/(M-(n*t))))-g*t, t, 'r-^') #second line changed
plt.xlabel('time')
plt.ylabel('velocity')
plt.show
here what I got!:

Categories