473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

I'm new to Python, as you can tell, and I'm interested in how to do this.
Jul 18 '05 #1
7 1307
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
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
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
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
On 2004-02-17, shivermetimbers15 <sh***************@yahoo.com> 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
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
DH
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AngleWyrm | last post by:
Ok, I know that srand( time(NULL) ) isn't such a good idea, but I do it anyway. Why? The clock is an easy source of randomness. And I've got this sneaky suspicion that I'm not the first guy to...
7
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
2
by: Peter Williams | last post by:
Hi All, I need to port the following (small) C language function to Delphi: double Rand(void) { return rand()/(1.0 + (double)RAND_MAX); } NB -- elsewhere in the program randomize is called...
4
by: Kamran K | last post by:
Hello I have created a client server application using C#. Existing application is using random number on client side to generate sequence numbers that are then assigned to transactions. This...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
14
by: Anthony Liu | last post by:
I am at my wit's end. I want to generate a certain number of random numbers. This is easy, I can repeatedly do uniform(0, 1) for example. But, I want the random numbers just generated sum up...
11
by: Amy | last post by:
Hello, I need to make this select an array item randomly instead of in order. Anyone know how? Thank you very much. Amy <BODY onload="changelink();"><a href="#" id="url"></a> var arr = ,, ,...
23
by: nsa.usa | last post by:
Hi, I used to use a function in other languages (TP or asm don't remember) where I could get number of clockticks since 1980. Is there a similar function in C? I don't seem to find it. I need...
9
by: Ken Fine | last post by:
Hi there, I have written a simple function that attempts to set the angle of objects so as to place them in aesthetically appealing ways. The code follows; there is some stupidness in it (e.g. a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.