0

I get a random number generating error such as,

Traceback (most recent call last): File "C:\Users\SONY\Desktop\deneme.py", line 34, in update(x) File "C:\Users\SONY\Desktop\deneme.py", line 12, in update x[j] = x[j] + uniform(-1.4,1.4) NameError: global name 'uniform' is not defined

My code is as follows

N = 20

N_cor = 25
N_cf = 25
a = 0.5
eps = 1.4

def update(x):
    for j in range(0,N):
        old_x = x[j]
        old_Sj = S(j,x)
        x[j] = x[j] + uniform(-eps,eps)
        dS = S(j,x) - old_Sj
        if dS>0 and exp(-dS)<uniform(0,1):
            x[j] = old_x

def S(j,x):
    jp = (j+1)%N
    jm = (j-1)%N
    return a*x[j]**2/2 + x[j]*(x[j]-x[jp]-x[jm])/a

def compute_G(x,n):
    g = 0
    for j in range(0,N):
        g = g + x[j]*x[(j+n)%N]
        return g/N


x=[]
for j in range(0,N):
    x.append(0.0)
    print"x(%d)=%f"%(j,x[j])
for j in range(0,5*N_cor):
    update(x)
for alpha in range(0,N_cf):
    for j in range(0,N_cor):
        update(x)
    for n in range(0,N):
        G[alpha][n] = compute_G(x,n)
for n in range(0,N):
    avg_G = 0
    for alpha in range(0,N_cf):
        avg_G = avg_G + G[alpha][n]
        avg_G = avg_G / N_cf
        print "G(%d) = %f"%(n,avg_G)

Can you help me about how can i generate a uniform random number in range (-eps,eps) ?

1
  • from random import uniform Commented Oct 20, 2015 at 19:56

2 Answers 2

2

You need to import random at the beginning of your code and call

random.uniform(a, b)

rather than just uniform.

Python docs on random number library

https://docs.python.org/2/library/random.html

Sign up to request clarification or add additional context in comments.

Comments

0

In the beginning of the code add the line from random import uniform This will cause you to call the uniform function of the random package every time you call uniform()

Another option is to add the line import random at the top of the file and replace every instance of uniform() with random.uniform()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.