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!

13 Years
Discussion Span
Last Post by shanenin

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!


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

choices = ('a','b','c','d','e','f','g','h','i','j','k')

this will choose one of the elments randomly.


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!


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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.