| | |
Python noob needs random help
![]() |
•
•
Join Date: Jun 2007
Posts: 2
Reputation:
Solved Threads: 0
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!
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.
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.
kath.
Last edited by katharnakh; Jun 28th, 2007 at 4:25 am.
•
•
Join Date: Jun 2007
Posts: 2
Reputation:
Solved Threads: 0
[QUOTE=katharnakh;395804]
but 1,2,3,4 does not seem random... they appear sequential. I am not sure I understand your question correctly.
[QUOTE]
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.
but 1,2,3,4 does not seem random... they appear sequential. I am not sure I understand your question correctly.
[QUOTE]
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.
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 4
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):
So what you need to do is write a loop which will add one random character for each character of the input sentence.
python Syntax (Toggle Plain Text)
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.
python Syntax (Toggle Plain Text)
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 )
Last edited by ffao; Jun 28th, 2007 at 10:50 am.
Hi,
if you want to know about sample then just type
Let me know whether it helps. Because im not sure i still understand your question...! its a bit confusing to me when you say, . what is the instance here...
kath.
Python Syntax (Toggle Plain Text)
>>>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'
kath.
Last edited by katharnakh; Jun 28th, 2007 at 11:17 am.
![]() |
Similar Threads
- Starting Python (Python)
- Starting a project in python (Game and quiz) (Python)
- How do you reset your variable everytime? (Python)
- Function that creates random changes in a string?? (Python)
Other Threads in the Python Forum
- Previous Thread: Reverse Data File
- Next Thread: how to hide querystring in asp.net2.0
| Thread Tools | Search this Thread |
address alarm anydbm app beginner cipher conversion coordinates curves cx-freeze data development dictionary directory dynamic examples excel feet file float format function generator getvalue gui halp handling homework images import input ip itunes java keycontrol line linux list lists loan loop maintain maze millimeter mouse mysqldb number numbers output parsing path port prime programming projects py2exe pygame pyglet pymailer python queue random recursion recursive screensaverloopinactive script scrolledtext searchingfile shebang slicenotation split ssh string strings table terminal text thread threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wx.wizard wxpython xlwt





