rss
logo

I provide consulting and custom development for Natural Language Processing, Information Extraction and Search solutions.Self Picture


 learn more   get in touch 

Logo - I Build Search
Apr 26
2009

Snippet to generate a random word in python digg

Since I haven’t posted here in a while, I figured I’d whip up this example real quick. It illustrates the usage of the random function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/python
import random
from time import time
 
def generateWord():
	char_array = 'abcdefghijklmnopqrstuvwxyz'
	random.seed(time())
	word = ''
	for i in range(0, 8): # 8 letter word
		word += char_array[random.randint(0, 25)]
	return word
 
if __name__ == '__main__':
	print generateWord()

2 Responses (rss) (trackback)

#1

DreamCatcher

May 9th, 2009 at 3:34 pm

man! its been long i have been in touch of coding..ur blog has changed so much!

#2

Evens

July 21st, 2009 at 7:56 am

May I submit another solution ?


#!/usr/bin/python
import random
import string

def generateWord():
char_array = string.ascii_lowercase
word = "".join(random.choice(char_array) for i in range(8))
return word

if __name__ == "__main__":
print generateWord()

Concerning the random.seed() function :

- You don’t need to random.seed() unless you need a specific seeding value.
- You don’t need to use time.time() because by default random.seed() will use the current time to seed the generator.
- You dont need to random.seed() since the generator will be seeded when the random module is imported.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Latest Articles

Feb
19

Join a list of integers in Python

How do you run a string join on a list of integers in Python? After googling for about 10 mins, I gave up and did this. I am sure there is a better way of doing it! [Read More]
Jan
21

Writing a spider in 10 mins using Scrapy

I came across Scrapy a few days back and have grown to really love it. This tutorial will illustrate how you can write a simple spider using Scrapy to scrape data off Paul Smith. All this in 10 minutes. [Read More]

Featured Projects

Indic to English Transliterator

Indic to English Transliterator

Transliteration is the process of converting a word from one language to another while retaining its phonetic characteristics. This application lets you convert a word from any major Indian language (currently supports Hindi, Marathi, Sanskrit and Bengali) to English.

[Read More]

Deebot

Deebot

Deeb0t is an IRC chat bot capable of making meaningful conversation with other users. It also responds to commands issued by its owner.

[Read More]

This page and its contents are copyright © 2010, Pravin Paratey.