Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Python] Function for random String
02-12-2010, 08:13 AM (This post was last modified: 02-12-2010 08:23 AM by Alphablend.)
Post: #1
[Python] Function for random String
You call the function entering any length of string that you wish.

Code:
def randstring(k):
    '''Program which randomizes a string with the entered length.'''
    from random import randint
    word=''
    for i in range(k):
        BS_letter=randint(0,1)#Big or small letter
        BS=randint(0,25)
        B_letter=65+BS
        S_letter=97+BS
        if BS_letter==0:
            word+=chr(S_letter)
        else:
            word+=chr(B_letter)
    return word

You will probably get the result like this one:

Code:
>>> randstring(100)
'ytIvRlLmKcVxXXBqospYvGmTXLlXpjJBFEFVRZvgUmMSppCdZnqiaLiKhDycFjXIdQLQvVFHJUTspxg​AndvUtnlUxfXINYcBSsgv'

You can go a step further and change the probability of getting the desired size of letters. Lets say we want our function to random upper case letters with twice bigger chance than lower case letters.
This is one nice way how to do it. You just have to change one tiny little thing in the code. I will just copy the line that you have to change.

Code:
BS_letter=randint(0,2)
Find all posts by this user
Quote this message in a reply
02-12-2010, 11:55 AM (This post was last modified: 02-12-2010 11:57 AM by codecaine.)
Post: #2
RE: [Python] Function for random String
Nice work this is how I did it. The string modules already have constant string etc you can use.

Code:
from string import letters;
from random import randint;

def randString(slen):
    word = '';
    for x in xrange(slen):
        word = word + letters[randint(0,len(letters)-1)];
    return word;

print(randString(100));
Visit this user's website Find all posts by this user
Quote this message in a reply
02-12-2010, 10:15 PM
Post: #3
RE: [Python] Function for random String
Very good. This is like some kind of random password generator. I made one in JavaScript :
http://www.pro9ramming.com/random-passwo...t-767.html
You could make that it asks you if you want to enter small letters or numbers or signs of Punctuation.
Very good work though !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
02-13-2010, 12:08 AM (This post was last modified: 02-13-2010 02:32 AM by Alphablend.)
Post: #4
RE: [Python] Function for random String
Thanks codecaine.
I did this like a classic Java programmer. Lol. I still don't know some benefits of Python. I imported random and checked all functions with 'help(random)', but didn't "catch" this one.

Actually I did have this task in Java. And decided to write it in Python.
Find all posts by this user
Quote this message in a reply
03-11-2010, 07:46 AM
Post: #5
RE: [Python] Function for random String
(02-13-2010 12:08 AM)Alphablend Wrote:  Thanks codecaine.
I did this like a classic Java programmer. Lol. I still don't know some benefits of Python. I imported random and checked all functions with 'help(random)', but didn't "catch" this one.

Actually I did have this task in Java. And decided to write it in Python.

Anytime Smile
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: