Python noob needs random help

Reply

Join Date: Jun 2007
Posts: 2
Reputation: anyedie is an unknown quantity at this point 
Solved Threads: 0
anyedie anyedie is offline Offline
Newbie Poster

Python noob needs random help

 
0
  #1
Jun 27th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: Python noob needs random help

 
0
  #2
Jun 28th, 2007
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.
Last edited by katharnakh; Jun 28th, 2007 at 4:25 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 27
Reputation: StrikerX11 is an unknown quantity at this point 
Solved Threads: 7
StrikerX11 StrikerX11 is offline Offline
Light Poster

Re: Python noob needs random help

 
0
  #3
Jun 28th, 2007
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)
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2
Reputation: anyedie is an unknown quantity at this point 
Solved Threads: 0
anyedie anyedie is offline Offline
Newbie Poster

Re: Python noob needs random help

 
0
  #4
Jun 28th, 2007
[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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: ffao is an unknown quantity at this point 
Solved Threads: 4
ffao ffao is offline Offline
Newbie Poster

Re: Python noob needs random help

 
0
  #5
Jun 28th, 2007
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):

  1. import random
  2. 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.

  1. import random
  2.  
  3. inp = 'aaaa' # sample input
  4. arr_choices = ('1', '2', '3', '4') # sample choices
  5.  
  6. #from here you have several choices for the loop...
  7.  
  8. #building a string letter to letter...
  9. result = ""
  10. for char in inp:
  11. result += random.choice(arr_choices)
  12.  
  13. #building a list of random chars
  14. res_list = []
  15. for char in inp:
  16. res_list.append(random.choice(arr_choices)
  17. #and joining it into one string...
  18. result = "".join(res_list)
  19.  
  20. #use a list comprehension so you can do the "building a list" method in one line
  21. result = "".join( random.choice(arr_choices) for char in inp )
Last edited by ffao; Jun 28th, 2007 at 10:50 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: Python noob needs random help

 
0
  #6
Jun 28th, 2007
Hi,

  1. >>>from random import sample
  2. >>>s='aaaa'
  3. >>>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.
  4. >>>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.
Last edited by katharnakh; Jun 28th, 2007 at 11:17 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC