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

Generate Random List of Integers

6
Hi,

I'm really new to python or really any programming at all. I'm trying to write a game and I'm really stuck on one small, seemingly easy task.

I have to generate a random list of n single digit integers (0 to 9).

For example, ['1', '5', '2', '8'] where n is 4.

Please help! I'm going out of my mind!

Thanks heaps
Aug 12 '08 #1
5 35126
bvdet
2,851 Expert Mod 2GB
Have you attempted to write any code? Hint: random.randrange() may be ideal for this task.
Aug 12 '08 #2
ss30
6
so far i have used

random.sample(range(0, 10), n) where n is the number of digits long.

This works really well for getting a random list with no repeats, but i need to have a random list where repeats are allowed.
Aug 12 '08 #3
boxfish
469 Expert 256MB
Hi,
You can use a for loop to loop n times, appending random numbers to your list. Is that what you want?
Hope this helps.
Aug 13 '08 #4
ss30
6
Hi,
You can use a for loop to loop n times, appending random numbers to your list. Is that what you want?
Hope this helps.

Yes, thankyou :) That helped a lot. I ended up using a while loop, but it seems to work well.

Thanks!
Aug 13 '08 #5
bvdet
2,851 Expert Mod 2GB
Yes, thankyou :) That helped a lot. I ended up using a while loop, but it seems to work well.

Thanks!
ss30,

The following uses a for loop and range() in a list comprehension to return a list of random integers:
Expand|Select|Wrap|Line Numbers
  1. def random_ints(num, lower=0, upper=9):
  2.     return [random.randrange(lower,upper+1) for i in range(num)]
  3.  
  4. print random_ints(4)
This is the equivalent code without the list comprehension:
Expand|Select|Wrap|Line Numbers
  1. def random_ints(num, lower=0, upper=9):
  2.     ints = []
  3.     for i in range(num):
  4.         ints.append(random.randrange(lower,upper+1))
  5.     return ints
Aug 13 '08 #6

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

Similar topics

5
by: Yudan YI \(OSU\) | last post by:
I want to generate random data from a normal distribution, while I checked the functions, and I found rand(), which returns a pseudorandom integer between zero and RAND_MAX. I am not sure how to...
4
by: Marc Dansereau | last post by:
Hi all, I wonder what is the most efficient way to generate random point on a line defined by 2 double points (x0,y0) and (x1,y1). Here is the pseudocode of my method : for each point {...
7
by: MattB | last post by:
I have the following code to generate random passwords for new users of an application. Const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstyvwxyz1234567890" Dim r As Int16, i As...
6
by: comp.lang.php | last post by:
/** * Generate the random security image * * @access public * @param $willUseFilePath (default false) boolean to determine if you will be using a file path * @param mixed $filePath (optional)...
0
sashi
by: sashi | last post by:
Generate Random Password In the course of programming you may have cause to generate a password. The following function will generate a password of randomly selected characters up to a maximum of...
9
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
5
by: DAXU | last post by:
Hi, I need to generate a fixed number of random integers that their summary equals to certain values. For example, the scenario can be: simulate user hits (e.g. 20000) within a hour (3600...
1
Srijith B
by: Srijith B | last post by:
HI all I am trying to write a perl script to generate random names ed. ( Qrhfh, Jdfhdh, Dfkjns) etc. So the script should ask me the number of random names and when I give say 4, It should give...
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:
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
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: 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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.