Random in python 2.5 not working?

I am trying to use the import random statement in python, but it doesn't appear to have any methods in it to use.

Am I missing something?


Asked by: Walter354 | Posted: 24-09-2021






Answer 1

You probably have a file named random.py or random.pyc in your working directory. That's shadowing the built-in random module. You need to rename random.py to something like my_random.py and/or remove the random.pyc file.

To tell for sure what's going on, do this:

>>> import random
>>> print random.__file__

That will show you exactly which file is being imported.

Answered by: Thomas783 | Posted: 25-10-2021



Answer 2

This is happening because you have a random.py file in the python search path, most likely the current directory.

Python is searching for modules using sys.path, which normally includes the current directory before the standard site-packages, which contains the expected random.py.

This is expected to be fixed in Python 3.0, so that you can't import modules from the current directory without using a special import syntax.

Just remove the random.py + random.pyc in the directory you're running python from and it'll work fine.

Answered by: Kevin832 | Posted: 25-10-2021



Answer 3

I think you need to give some more information. It's not really possible to answer why it's not working based on the information in the question. The basic documentation for random is at: https://docs.python.org/library/random.html

You might check there.

Answered by: Nicole457 | Posted: 25-10-2021



Answer 4

Python 2.5.2 (r252:60911, Jun 16 2008, 18:27:58)
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> random.seed()
>>> dir(random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', '_BuiltinMethodType', '_MethodType', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '_acos', '_ceil', '_cos', '_e', '_exp', '_hexlify', '_inst', '_log', '_pi', '_random', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'uniform', 'vonmisesvariate', 'weibullvariate']
>>> random.randint(0,3)
3
>>> random.randint(0,3)
1
>>>  

Answered by: Catherine773 | Posted: 25-10-2021



Answer 5

If the script you are trying to run is itself called random.py, then you would have a naming conflict. Choose a different name for your script.

Answered by: Sophia239 | Posted: 25-10-2021



Answer 6

Can you post an example of what you're trying to do? It's not clear from your question what the actual problem is.

Here's an example of how to use the random module:

import random
print random.randint(0,10)

Answered by: Sawyer109 | Posted: 25-10-2021



Answer 7

Seems to work fine for me. Check out the methods in the official python documentation for random:

>>> import random
>>> random.random()
0.69130806168332215
>>> random.uniform(1, 10)
8.8384170917436293
>>> random.randint(1, 10)
4

Answered by: Dexter898 | Posted: 25-10-2021



Answer 8

Works for me:

Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51) 
[GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> brothers = ['larry', 'curly', 'moe']
>>> random.choice(brothers)
'moe'
>>> random.choice(brothers)
'curly'

Answered by: Ada364 | Posted: 25-10-2021



Similar questions

Random Forest not working in opencv python (cv2)

I can't seem to correctly pass in the parameters to train a Random Forest classifier in opencv from python. I wrote an implementation in C++ which worked correctly, but do not get the same results in python. I found some sample code here: http://fossies.org/linux/misc/opencv-2.4.7.tar....


random - 'for char in word:' not working in Python

I'm making a simple hangman game and I've got a word list on my computer with about 5,000 word, I made a list out of those words then used the random function to choose one at random for the game, but it prints one too many of these: '-', when trying to guess the word, I used the command 'for char in word:' to count the characters and print the right amount of '-'. import time import random List = open("no...


c++ - Hello World from cython wiki not working

I'm trying to follow this tutorial from Cython: http://docs.cython.org/docs/tutorial.html#the-basics-of-cython and I'm having a problem. The files are very simple. I have a helloworld.pyx: print "Hello World" and a setup.py: from distutils.core import setup from d...


HTML forms not working with python

I've created a HTML page with forms, which takes a name and password and passes it to a Python Script which is supposed to print the persons name with a welcome message. However, after i POST the values, i'm just getting the Python code displayed in the browser and not the welcome message. I have stored the html file and python file in the cgi-bin folder under Apache 2.2. If i just run a simple hello world python script in...


xml - Python xpath not working?

Okay, this is starting to drive me a little bit nuts. I've tried several xml/xpath libraries for Python, and can't figure out a simple way to get a stinkin' "title" element. The latest attempt looks like this (using Amara): def view(req, url): req.content_type = 'text/plain' doc = amara.parse(urlopen(url)) for node in doc.xml_xpath('//title'): req.write(str(node)+'\n') ...


python - Find the file with a given SVN URL within a SVN working copy

Given a starting point in a Subversion working copy (e.g. current working directory), and a target SVN URL, I'd like to find the file in the working copy that has that SVN URL. For example, given this current directory: c:\Subversion\ProjectA\a\b\c\ which has this SVN URL: https://svnserver/svn/ProjectA/trunk/a/b/c/ I'd like to locate the file on t...


Does anyone know a "working" Python library that can read .ARC files?

Closed. This question does not meet Stack Overflow guid...


Why is this simple python class not working?

I'm trying to make a class that will get a list of numbers then print them out when I need. I need to be able to make 2 objects from the class to get two different lists. Here's what I have so far class getlist: def newlist(self,*number): lst=[] self.number=number lst.append(number) def printlist(self): return lst Sorry I'm not very clear, I'm a...


Working with JSON in Python 2.6?

I'm really new to Python, but I've picked a problem that actually pertains to work and I think as I figure out how to do it I'll learn along the way. I have a directory full of JSON-formatted files. I've gotten as far as importing everything in the directory into a list, and iterating through the list to do a simple print that verifies I got the data. I'm trying to figure out how to actually work with a gi...


python - Scrapy spider is not working

Since nothing so far is working I started a new project with python scrapy-ctl.py startproject Nu I followed the tutorial exactly, and created the folders, and a new spider from scrapy.contrib.spiders import CrawlSpider, Rule from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor from scrapy.selector import HtmlXPathSelector from scrapy.item import Item from Nu.i...


python - Working with bit streams

I have a base64 encoded bit stream, I want to work with. After decoding it with base64.b64decode I get a bytes object (Py3k btw) containing the decoded code. The problem is now, that I would like to work on that bytes object with bit-wise operations, such as shifting, bit wise and etc, but that is not possible as it is a bytes sequence. Is there any way to use binary operations on that dec...


python - pydev 1.5.3 not working fine with Easy Eclipse 1.3.1

I installed Pydev 1.5.3 (so that I could get the merged version of Pydev Extensions in core PyDev) in an EasyEclipse 1.3.1 installation. After this, Compare with > Base revision etc. comparison operations stopped working. I had to disable the PyDev 1.5.3 and revert back to the pre-installed Pydev 1.3.13 (part of EasyEclipse 1.3.1). Has anybody faced similar problem? Is there any work-around for thi...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top