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!

Recommended Answers

All 5 Replies

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')
random.choice(choices)

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...

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.