443,846 Members | 1,862 Online
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

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

 P: n/a I'm new to Python, as you can tell, and I'm interested in how to do this. Jul 18 '05 #1
7 Replies

 P: n/a import random def rand_seq(): while True: yield random.random() for rn in rand_seq(): print rn -- Regards, Diez B. Roggisch Jul 18 '05 #2

 P: n/a sh***************@yahoo.com (shivermetimbers15) writes: 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. Jul 18 '05 #3

 P: n/a shivermetimbers15 wrote: 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 Jul 18 '05 #4

 P: n/a Peter Hansen wrote: [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 Jul 18 '05 #5

 P: n/a On 2004-02-17, shivermetimbers15 wrote: 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. Jul 18 '05 #6

 P: n/a sh***************@yahoo.com (shivermetimbers15) wrote in message news:<8e**************************@posting.google. com>... 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'] Jul 18 '05 #7

 P: n/a Peter Hansen wrote: 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. Jul 18 '05 #8