473,385 Members | 1,347 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,385 software developers and data experts.

Random number generator

Thekid
145 100+
I've been playing with the random module but can't seem to get my code to print out more than one random number. How do I go about that? I 've tried several things but haven't figured it out yet.

Expand|Select|Wrap|Line Numbers
  1.  
  2. from random import randint
  3. a = randint(0,40)
  4. print a
  5.  
  6.  
or

Expand|Select|Wrap|Line Numbers
  1. import random
  2. print random.randrange(0,40,1)
  3.  
That works fine but how can I get it to print out 3 numbers? Example:

3, 16, 23

I did find this code below which works for this purpose but I figure there has to be an easier way:

Expand|Select|Wrap|Line Numbers
  1. # File: random-example-4.py
  2.  
  3. import random
  4.  
  5. try:
  6.     # available in Python 2.0 and later
  7.     shuffle = random.shuffle
  8. except AttributeError:
  9.     def shuffle(x):
  10.         for i in xrange(len(x)-1, 0, -1):
  11.             # pick an element in x[:i+1] with which to exchange x[i]
  12.             j = int(random.random() * (i+1))
  13.             x[i], x[j] = x[j], x[i]
  14.  
  15. cards = range(52)
  16.  
  17. shuffle(cards)
  18.  
  19. myhand = cards[:5]
  20.  
  21. print myhand
  22.  
Aug 5 '08 #1
2 2438
Hey try this if it suits ur requirement
I Have hardcoded 2 since u wanted only 3 numbers, modify it to ur need

Expand|Select|Wrap|Line Numbers
  1. import random
  2. randlist=[]
  3. while (randlist.__len__() <= 2):
  4.   b =random.randint(0,25)
  5.   if not b in randlist:
  6.      randlist.append(b)
  7. print randlist
Aug 6 '08 #2
Thekid
145 100+
Hey try this if it suits ur requirement
I Have hardcoded 2 since u wanted only 3 numbers, modify it to ur need

Expand|Select|Wrap|Line Numbers
  1. import random
  2. randlist=[]
  3. while (randlist.__len__() <= 2):
  4.   b =random.randint(0,25)
  5.   if not b in randlist:
  6.      randlist.append(b)
  7. print randlist
Very nice, thank you!
Aug 6 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
16
by: raylopez99 | last post by:
For the public record. RL public void IterateOne() { Random myRandom = new Random(); //declare Random outside the iteration for (int j = 0; j < Max; j++) {
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.