I have a question.

B

Brian van den Broek

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/
looking at the docs" %random.random()
It took 0.31385101929 seconds to find the module named random by
looking at the docs
 
J

Jeremy Jones

Nathan said:
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
 
J

Jags

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:

Hope this helps.

Cheers!
-- Jags.
 
N

Nathan Pinno

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 said:
> 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top