How do make a function that creates a sequence of n random numbers?

S

shivermetimbers15

I'm new to Python, as you can tell, and I'm interested in how to do this.
 
D

Diez B. Roggisch

import random

def rand_seq():
while True:
yield random.random()

for rn in rand_seq():
print rn
 
P

Paul Rubin

I'm new to Python, as you can tell, and I'm interested in how to do this.

What other languages do you use and what do you want the random
numbers for? That might be of some help in understanding what level
to answer your question at.

The simplest answer is something like

import random # load python's random number module
seq = [random.random() for i in xrange(n)] # generate sequence of length n

but that may not be what you want.
 
P

Peter Hansen

shivermetimbers15 said:
I'm new to Python, as you can tell, and I'm interested in how to do this.

1. Go through the Python tutorial step by step.

2. Read the online docs and learn about the "random" module.

3. Write the function using a simple for loop and xrange(),
and appending the results of calls to random.randint() to
a list, returning the list when you're done.

If you need more help, I suggest you post the code you have written so
far to this mailing list/newsgroup and ask for feedback. You'll learn
the most, the fastest, this way, and will demonstrate your own willingness
to do some of the work.

-Peter
 
P

Peter Hansen

Peter said:
[snip]
If you need more help, I suggest you post the code you have written so
far to this mailing list/newsgroup and ask for feedback. You'll learn
the most, the fastest, this way, and will demonstrate your own willingness
to do some of the work.

Or, just wait a moment and someone will post a working example, robbing
you of the opportunity to learn how to do it for yourself. :)

-Peter
 
L

Lee Harr

I'm new to Python, as you can tell, and I'm interested in how to do this.


Sounds suspiciously like homework to me...

Post some code you've written and I am sure people will help you
to get it working.
 
A

Asun Friere

I'm new to Python, as you can tell, and I'm interested in how to do this.


As well as using random.random() consider whether some of the other
methods in the random module might serve your needs, eg.
import random
random.randrange(500,1000) 806
[random.randrange(0, 10) for n in range(8)] [4, 1, 6, 5, 5, 3, 0, 5]
a = ['one', 'two', 'three', 'four', 'five']
random.choice(a) 'three'
random.shuffle(a)
a
['four', 'two', 'one', 'three', 'five']
 
D

DH

Peter said:
If you need more help, I suggest you post the code you have written so
far to this mailing list/newsgroup and ask for feedback. You'll learn
the most, the fastest, this way, and will demonstrate your own willingness
to do some of the work.

Maybe the question was asking for help getting started.
 
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

shivermetimbers15 said:
I'm new to Python, as you can tell, and I'm interested in how to do this.

Do you want to create your own random number generator, or do you just want to use
an existing one?

If you want to use a good one, you can just use the one in the Python standard
library in the module 'random'.

-- Gerhard
 
P

Peter Hansen

DH said:
Maybe the question was asking for help getting started.

The answer I provided gave him ample help, if he was interested in
doing it himself. If he just wanted the answer on a silver platter,
other reponses were provided which "helped" in that way... his choice.

-Peter
 
S

shivermetimbers15

Thanks, everyone. Also, I'd really like to know how to remove the
lowest number from the sequence.
 
A

Amy G

Post some code - or even psedo code of how you think this could be
accomplished.

You may want to look at the methods for a list to help get you started.
Just google "python list methods."
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top