954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Function that creates random changes in a string??

Hello!!

Does anybody know how to create a function that randomly changes elements in a string?
And: does anybody know how to set the chances for the type of changes?
I've tried to look it up in tutorials, but since I'm a beginner, most of the time creating random outputs, requires a lot more Python knowledge than I currently have.

Thanks in advance!

Lee Bel
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Btw, I think/hope that random.randrange could be helpful, but can't really figure out how I can make it work if I don't want to use integers...
Anyway; a link to a helpful tutorial would already be very much appreciated!

Lee Bel
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

what kind of changes are you looking for. lets say you have the string 'the dog went home' , how would you want that changed? Changed is a vague term.

you may want to use random.choice it will make a random choice of an element.

here is an example

[php]
choices = ('a','b','c','d','e','f','g','h','i','j','k')
random.choice(choices)
[/php]
this will choose one of the elments randomly.

shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

There are three types of changes I want to make: deletion, insertion and replacement. I do know how these changes can be made with slices.
But the real problems are:
1) I can't get the changes to be completely random, while still asking for a string input and not integers.
2) When a change occurs (the user can request the number of changes), the type of changes should occur with certain chance ratios. I haven't found such a function yet in Python.

I am going to see what I can work out with random.choice. Thank you for your reply!

Lee Bel
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

The program is completed!
Thank you for the tip about random.choice! That was the missing link I couldn't get working...

Lee Bel
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

your welcome. I know very little, but am glad I had something to offer.

shanenin
Posting Whiz in Training
217 posts since May 2005
Reputation Points: 10
Solved Threads: 17
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You