By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

I have a question.

P: n/a


Hi all,

Does Python have a random function? If so, can you show me an example
using it?

Thanks,
Nathan Pinno
http://www.npinnowebsite.ca/

--
----------------------------------------------------------------
Posted via UsenetRevolution.com - Revolutionary Usenet
** HIGH RETENTION ** Specializing in Large Binaries Downloads **
http://www.UsenetRevolution.com
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Nathan Pinno wrote:
Does Python have a random function? If so, can you show me an example
using it?


http://docs.python.org/lib/module-random.html
--
Benji York
Jul 19 '05 #2

P: n/a
Nathan Pinno said unto the world upon 30/06/2005 11:22:

Hi all,

Does Python have a random function? If so, can you show me an example
using it?

Thanks,
Nathan Pinno
http://www.npinnowebsite.ca/

import random
print "It took %s seconds to find the module named random by looking at the docs" %random.random()
It took 0.31385101929 seconds to find the module named random by
looking at the docs

Jul 19 '05 #3

P: n/a
Nathan Pinno wrote:
Hi all,

Does Python have a random function? If so, can you show me an example
using it?

Thanks,
Nathan Pinno
http://www.npinnowebsite.ca/


Take your pick:

In [5]: import random

In [6]: random.choice(range(10))
Out[6]: 2

In [7]: random.choice(range(10))
Out[7]: 7

In [8]: random.choice(range(10))
Out[8]: 8

In [9]: random.choice(range(10))
Out[9]: 8
In [14]: random.random()
Out[14]: 0.56386154889489271

In [15]: random.random()
Out[15]: 0.47322827346926843

In [16]: random.random()
Out[16]: 0.39921336622176518

In [17]: random.random()
Out[17]: 0.65521407248459007

In [18]: random.random()
Out[18]: 0.74525381787627598
In [20]: r = range(10)

In [21]: random.shuffle(r)

In [22]: r
Out[22]: [6, 4, 9, 7, 2, 0, 8, 3, 5, 1]
Jeremy Jones
Jul 19 '05 #4

P: n/a
Hi,

You can use the "Random" module. You need to first import it using
"from random import Random, random".

Try the following sequence of steps on the Python/IDLE command line to
get an idea:
from random import Random, random
myRandom = Random()
myRandom.random()

Hope this helps.

Cheers!
-- Jags.

Nathan Pinno wrote: Hi all,

Does Python have a random function? If so, can you show me an example
using it?

Thanks,
Nathan Pinno
http://www.npinnowebsite.ca/

--
----------------------------------------------------------------
Posted via UsenetRevolution.com - Revolutionary Usenet
** HIGH RETENTION ** Specializing in Large Binaries Downloads **
http://www.UsenetRevolution.com


Jul 19 '05 #5

P: n/a
Thanks all.

I needed to ask because I'm writing a couple of programs which require the
computer to choose numbers at random thanks.

Nathan Pinno
"Jeremy Jones" <za******@bellsouth.net> wrote in message
news:ma***************************************@pyt hon.org...
Nathan Pinno wrote:
Hi all,

Does Python have a random function? If so, can you show me an example
using it?

Thanks,
Nathan Pinno
http://www.npinnowebsite.ca/


Take your pick:

In [5]: import random

In [6]: random.choice(range(10))
Out[6]: 2

In [7]: random.choice(range(10))
Out[7]: 7

In [8]: random.choice(range(10))
Out[8]: 8

In [9]: random.choice(range(10))
Out[9]: 8
In [14]: random.random()
Out[14]: 0.56386154889489271

In [15]: random.random()
Out[15]: 0.47322827346926843

In [16]: random.random()
Out[16]: 0.39921336622176518

In [17]: random.random()
Out[17]: 0.65521407248459007

In [18]: random.random()
Out[18]: 0.74525381787627598
In [20]: r = range(10)

In [21]: random.shuffle(r)

In [22]: r
Out[22]: [6, 4, 9, 7, 2, 0, 8, 3, 5, 1]
Jeremy Jones


--
----------------------------------------------------------------
Posted via UsenetRevolution.com - Revolutionary Usenet
** HIGH RETENTION ** Specializing in Large Binaries Downloads **
http://www.UsenetRevolution.com
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.