473,396 Members | 2,106 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.

Random numbers and alphabet mixture

Hi,

I am a noob and I was wondering if there is way to generate random mixture of alphabets and numbers? For example:

1234567890ABCDEFG
or
4BA92ABD293BC3890

the length needs to be 17 characters long.
Mar 24 '08 #1
8 3019
jlm699
314 100+
Hi,

I am a noob and I was wondering if there is way to generate random mixture of alphabets and numbers? For example:

1234567890ABCDEFG
or
4BA92ABD293BC3890

the length needs to be 17 characters long.
How about this list comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> ''.join([chr([random.randrange(48,58),random.randrange(65,91)][random.randint(0,1)]) for x in xrange(17)])
  2. '3E4F8F756F584Z8XU'
  3.  
Or in non-comprehension form:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. rstring = ''
  4. for x in xrange(17):
  5.     rint = random.randint(0,1)
  6.     if rint:
  7.         rstring += chr(random.randrange(48,58))
  8.     else:
  9.         rstring += chr(random.randrange(65,91))
  10.  
  11. >>> rstring
  12. 'T374TX07KEP031F17'
  13.  
Mar 24 '08 #2
bvdet
2,851 Expert Mod 2GB
Hi,

I am a noob and I was wondering if there is way to generate random mixture of alphabets and numbers? For example:

1234567890ABCDEFG
or
4BA92ABD293BC3890

the length needs to be 17 characters long.
That is easily done with the random.choice() function, a list or string of character choices, and a list comprehension or for loop. Select a character from the choices 17 times, and join or concatenate the characters. Try coding yourself and post back.

Example output from a variable characters, 10 random strings:

>>> characters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ0123456789'
>>> j5jSeP4etag0EucN0
BAlxpfUfXnEjBNaL6
dB0GSaCFZMRg2DrqK
uMLz8ivN3cm8k8MCC
b6qHuPAf7ATYsLIjd
cfYmLEfGa66nKZzlJ
ldQd3ouLNSG7ghSu3
vxFn9UHchY8nRe2Mj
QfbuEMtfjRZJuxALZ
18TcORlqnZGrOUUrm
>>>
Mar 24 '08 #3
bvdet
2,851 Expert Mod 2GB
Now that we have some code in this thread, I guess I'll post mine :)
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> import string
  3. >>> characters = string.letters+string.digits
  4. >>> no_chrs = 17
  5. >>> print ''.join([random.choice(characters) for _ in range(no_chrs)])
  6. uGAGd5Er82VOFsjwh
  7. >>> 
Mar 24 '08 #4
thanks guys for the help.
Mar 24 '08 #5
[quote=jlm699]How about this list comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> ''.join([chr([random.randrange(48,58),random.randrange(65,91)][random.randint(0,1)]) for x in xrange(17)])
  2. '3E4F8F756F584Z8XU'
  3.  
I am trying to understand this. So, you have a chr range of 0-9[48,58] and A-Z[65-91], but what is random.randint(0,1) is that step by one? I don't understand this. I like how this is written, but hopefully someone can explain.
Mar 24 '08 #6
jlm699
314 100+
what is random.randint(0,1) is that step by one? I don't understand this.
No that is an obfuscated way to randomly choose between one of two choices.
Let's say you have a list tst = ['a', 'b']
Element 0 (tst[0]) is 'a' and element 1 (tst[1]) is 'b'. So let's say instead of storing the list to a variable tst you simply use the list itself:
Expand|Select|Wrap|Line Numbers
  1. >>> ['a', 'b'][0]
  2. 'a'
  3. >>> ['a', 'b'][1]
  4. 'b'
  5.  
Now if we put random.randint(0,1) in place of the index specifier, we have a randomly chosen element from the list picked
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> ['a', 'b'][random.randint(0,1)]
  3. 'b'
  4. >>> ['a', 'b'][random.randint(0,1)]
  5. 'a'
  6. >>> 
So in my code example I have the random letter and the random number pickers contained in a ad-hoc list [random_letter, random_number] and an element is chosen based on the result of randint(0,1). Does that clear it up a little bit? Unfortunately, this is not the best practice in terms of computing efficiency as a random letter and two random numbers are generated on each iteration (actually three random numbers), whereas bvdet's solution only requires one random number (index of list) to be generated.
Mar 24 '08 #7
bvdet
2,851 Expert Mod 2GB
How about this list comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> ''.join([chr([random.randrange(48,58),random.randrange(65,91)][random.randint(0,1)]) for x in xrange(17)])
  2. '3E4F8F756F584Z8XU'
  3.  
I am trying to understand this. So, you have a chr range of 0-9[48,58] and A-Z[65-91], but what is random.randint(0,1) is that step by one? I don't understand this. I like how this is written, but hopefully someone can explain.
Breaking it up into two parts:
Expand|Select|Wrap|Line Numbers
  1. >>> (chr(random.randrange(48,58)),chr(random.randrange(65,91)))
  2. ('6', 'O')
  3. >>> (chr(random.randrange(48,58)),chr(random.randrange(65,91)))
  4. ('8', 'Y')
  5. >>> random.randint(0,1)
  6.  
  7. >>> random.randint(0,1)
  8. 1
  9. >>> (chr(random.randrange(48,58)),chr(random.randrange(65,91)))[random.randint(0,1)]
  10. 'T'
  11. >>> (chr(random.randrange(48,58)),chr(random.randrange(65,91)))[random.randint(0,1)]
  12. 'R'
  13. >>> (chr(random.randrange(48,58)),chr(random.randrange(65,91)))[random.randint(0,1)]
  14. '3'
  15. >>> 
#1 - Create a tuple containing a letter and a number
#2 - Take a slice of the tuple (using random.randint(0,1)) to produce a random letter or number
Mar 24 '08 #8
Thanks that helps. Makes sense now.

No that is an obfuscated way to randomly choose between one of two choices.
Let's say you have a list tst = ['a', 'b']
Element 0 (tst[0]) is 'a' and element 1 (tst[1]) is 'b'. So let's say instead of storing the list to a variable tst you simply use the list itself:
Expand|Select|Wrap|Line Numbers
  1. >>> ['a', 'b'][0]
  2. 'a'
  3. >>> ['a', 'b'][1]
  4. 'b'
  5.  
Now if we put random.randint(0,1) in place of the index specifier, we have a randomly chosen element from the list picked
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> ['a', 'b'][random.randint(0,1)]
  3. 'b'
  4. >>> ['a', 'b'][random.randint(0,1)]
  5. 'a'
  6. >>> 
So in my code example I have the random letter and the random number pickers contained in a ad-hoc list [random_letter, random_number] and an element is chosen based on the result of randint(0,1). Does that clear it up a little bit? Unfortunately, this is not the best practice in terms of computing efficiency as a random letter and two random numbers are generated on each iteration (actually three random numbers), whereas bvdet's solution only requires one random number (index of list) to be generated.
Mar 24 '08 #9

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

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
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...
11
by: Olaf \El Blanco\ | last post by:
How can i generate random words? ('a'..'z') Is there any function that convert a number to it ascci char? My english is horrible! Here an example: function(65) return 'a'; Thank you!
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
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 ...
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
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?
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
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...
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...

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.