6

i am having a weird issue,

i have created the following code to randomly generate numbers between 1 and x with increments of 1 and to store them

import random

bootstrap_node_list_recieved = [] #List of all nodes addresses recieved during the bootstrap peroid - Is a list so we can compare duplicatition probability etc
average_getAdrr_no_node_response = 100 #Number or nodes typically sent when a node requests a getAddr message
network_ip_node_size = 5000 # Number of IP addresses / nodes that have been seen on the network in the past 2 weeks


#Move into calculations.py when ready
#Number of nodes recieved (Bootstrap)
def bootstrap_node_getAddr():
    #### TODO ####
    #Random generation of nodes (number represents a single node), from 1 to x for an average amount of nodes
    # node_list=[random.randrange(1,network_ip_node_size,1) for _ in range (average_getAdrr_no_node_response)]
    for i in range (average_getAdrr_no_node_response):
        bootstrap_node_list_recieved.append(random.randrange(1,network_ip_node_size,1))
    print 'bootstrap_node_getAddr: ', bootstrap_node_list_recieved
    # return bootstrap_node_list_recieved

bootstrap_node_getAddr()

this code is working fine on its own, however when i insert it into my main code base i get the error

Traceback (most recent call last):
  File "BootstrapBTC.py", line 117, in query_dns_servers
    bootstrap_node_getAddr()
  File "/home/richard/Dropbox/PhD/Simulator BTC - Ours/Calculations.py", line 33, in bootstrap_node_getAddr
    bootstrap_node_list_recieved.append(random.randrange(1,network_ip_node_size,1))
AttributeError: 'builtin_function_or_method' object has no attribute 'randrange'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "BootstrapBTC.py", line 90, in run
    yield self.env.process(query_dns_servers(env, self))
AttributeError: 'builtin_function_or_method' object has no attribute 'randrange'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "BootstrapBTC.py", line 178, in <module>
    env.run()
  File "/usr/local/lib/python2.7/dist-packages/simpy/core.py", line 137, in run
    self.step()
  File "/usr/local/lib/python2.7/dist-packages/simpy/core.py", line 229, in step
    raise exc
AttributeError: 'builtin_function_or_method' object has no attribute 'randrange

the code on line 90 is

    yield self.env.process(query_dns_servers(env, self))

which just calls

#Average respsonse time from a DNS server
def DnsServerResponse(env, self):
    yield self.env.timeout(dns_average_response)

which appends a random value into the simulation time, i do not think it has anything to do with this line of code since it doesn't use the random library at all, infact the code inserted is the only thing so far which uses the library

anyone got any ideas what the issue is ? it is driving me mad !

Thanks !

tried changing my import to just import random, and now get :

Traceback (most recent call last):
  File "BootstrapBTC.py", line 102, in query_dns_servers
    DnsUp = DnsUpProbability()
  File "/home/richard/Dropbox/PhD/Simulator BTC - Ours/Calculations.py", line 39, in DnsUpProbability
    up = (0 if random() > Prob_DNS_UP else 1)
TypeError: 'module' object is not callable
user4017041
  • 113
  • 1
  • 1
  • 8

4 Answers4

8

The error is because you most likely assigned random to another value somewhere in your code overriding the original random of the random module.

Try checking your code over to see what else you assigned random to. Or you can check it by doing so: before where you used random

print(random, type(random))

Another way to fix this is if your code is too long and hard to check over, you can import random like this: using import...as... format.

import random as rand
# and when using it, type rand instead of random
bootstrap_node_list_recieved.append(rand.randrange(1,network_ip_node_size,1))

You should also check to see if there's any file named random.py in your sys.path.

Taku
  • 31,927
  • 11
  • 74
  • 85
  • 1
    print(random, type(random)) gives (, ) the import solution as well doesn't work, have checked and no other values have been assigned to random – user4017041 Mar 26 '17 at 01:45
  • print(random, type(random)) gives (, ) the import solution as well doesn't work for me either. is this a python 2 only solution? – Magnus May 16 '20 at 17:38
  • 1
    @Magnus it’s a universal solution. For your case, instead of `from random import random` do `import random` – Taku May 17 '20 at 12:04
  • Thanks Taku, `import random` works. `from random import random` occurs the error. – kujiy Nov 17 '21 at 09:19
2

anyone got any ideas what the issue is ? it is driving me mad !

The usual cause of this error is that someone named a file "random.py" and that is being read instead of the one in the standard library.

An easy way to test for this is to look to see where random comes from:

print(random.__file__)

The other possible cause is that random got assigned some other value after the import random. Try checking:

print(type(random))         # This should be a module object.

Also try:

help(random)                # To see what this object is.

Here's as example that would give exactly the error message you're seeing:

>>> from random import random
>>> random.rangerange()
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    random.rangerange()
AttributeError: 'builtin_function_or_method' object has no attribute 'rangerange'

In this case, the solution is to convert from random import random to just import random.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
0

I was having this error. I realized it was due to a conflicting module. Specifically, pylab. pylab has a random function, so it conflicted. For me the solution is to only import the functions I need from pylab.

So rather than from pylab import *

I'll now write from pylab import blankityblankblank

Erico9001
  • 75
  • 4
-1

I also meet the error, and I find that I have double imported random. First, I use import random ,but in another *.py file, I use from random import random.