Im pretty much a python and for that matter a programming noob. I will learn and little, then quit, learn some more, and quit again. The past few times i've been working on the same project, and mostly keep giving up on python because I cannot seem to find an answer for it; so after that long winded introduction here comes the thread:

How do you give a different random result for the same input. For example if I wanted to write a script that would convert inputted characters to a random selection of outputted characters, even if it were the same character.
More example if that was not clear:
I want to convert the inputted sequence 'aaaa' to a random choice of either 1,2,3,4.

All my attempts to do this or find out how to do this have resulted in '1111', '2222', '3333', ect.

Help!

I want to convert the inputted sequence 'aaaa' to a random choice of either 1,2,3,4.

could you please explain in detail what you want to know??

but 1,2,3,4 does not seem random... they appear sequential. I am not sure I understand your question correctly.

BTW, how did you arrive to this?

All my attempts to do this or find out how to do this have resulted in '1111', '2222', '3333', ect.

If you can give better explanation of what you want to know, that would be better to get reply.

kath.

I'll give you a hint

1- Count the letters of the input string
2- populate an array of integers with random numbers in range(0, count)
3- Happy ending (Avril lavigne) :)

but 1,2,3,4 does not seem random... they appear sequential. I am not sure I understand your question correctly.


The 1,2,3,4 was just ment to reffer to any random choice of a few different variables.

I want a user of the program to be able to put in a string and the program to replace each instance of a certain letter with a different random choice.

If a user inputted the string 'aaaa' and I was using a random choice from the array [0,2,4,6] I would want the string to be outputted as each instance of 'a' replaced by a different random choice.

Sorry for the confusion, but I just cannot figure this out.

If you have an array of choices arr_choices, it's trivial to get a random element from it (I advise you to take a look at the documentation for the random module, if you haven't already):

import random
random.choice(arr_choices)

So what you need to do is write a loop which will add one random character for each character of the input sentence.

import random

inp = 'aaaa' # sample input
arr_choices = ('1', '2', '3', '4') # sample choices

#from here you have several choices for the loop...

#building a string letter to letter...
result = ""
for char in inp:
    result += random.choice(arr_choices)

#building a list of random chars
res_list = []
for char in inp:
    res_list.append(random.choice(arr_choices)
#and joining it into one string...
result = "".join(res_list)

#use a list comprehension so you can do  the "building a list" method in one line
result = "".join( random.choice(arr_choices) for char in inp )

Hi,

>>>from random import sample
>>>s='aaaa'
>>>array=['0','2','4','6']# I insist the array should a character array. If you give large array then the chance of shuffle is more. 
>>>print ''.join( random.sample(array, len(s)) )

if you want to know about sample then just type sample.__doc__ Let me know whether it helps. Because im not sure i still understand your question...! its a bit confusing to me when you say,

each instance of 'a'

. what is the instance here...


kath.

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.