Skip to content

I learning Python – Random strings

July 1, 2009

[ptBR]
Simples script que gera strings com valores randômicos


#!/usr/bin/env python
# Script to generate random strings - iarly selbir | ski0s

import random

def strgen():
string = ''
rand_str = random.sample('abcdefghijklqmnopqrstuvwxyz0123456789',8)
for i in rand_str:
string+=i
return string

print strgen()

Copie, cole e salve-o como strgen.py e execute-o para ver o resultado:
# python strgen.py

Exemplo:

[iarlyy@ski0s py]$ python strgen.py
wgrklj8m

[enUS]

Simple script that generate strings with random values


#!/usr/bin/env python
# Script to generate random strings - iarly selbir | ski0s

import random

def strgen():
string = ''
rand_str = random.sample('abcdefghijklqmnopqrstuvwxyz0123456789',8)
for i in rand_str:
string+=i
return string

print strgen()

Copy it, paste and save them as strgen.py and execute them to see the result:
# python strgen.py

Example:

[iarlyy@ski0s py]$ python strgen.py
wgrklj8m

From → Python

One Comment
  1. Linus permalink

    I could be a lot simpler. Consider this:

    import random
    print ”.join(random.sample(‘abcdefghijklqmnopqrstuvwxyz0123456789’,8))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: