473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Random Letters

Simple question here. Im trying to figureout how to write a program in such a way that when a word is entered, the output is each individual letter of that word in a random order. so if pizza is entered, the output would be
P
Z
A
I
Z

I have a general idea of how to output the letters, but not without them repeating. Id like to figure this out myself, but would appreciate being pointed in the right direction. heres the code thus far:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. word = "index"
  4. print "The word is: ", word, "\n"
  5.  
  6. high = len(word)
  7. low = -len(word)
  8. x=len(word)
  9.  
  10.  
  11. for i in range(x):
  12.     position = random.randrange(low, high)
  13.     print "word[", position, "]\t", word[position]
  14.  
  15. raw_input("\n\nPress the enter key to exit.")
  16.  
thanks
Sep 19 '07 #1
10 5099
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. >>> word = "hello"
  2. >>> x = len(word)
  3. >>> beenUsed = [] # and empty list of used indeces
  4. >>> while len(beenUsed) < x:
  5. ...     position = random.randint(0, x - 1)
  6. ...     if position not in beenUsed:
  7. ...         beenUsed.append(position)
  8. ...         print "word[", position, "]\t", word[position]
  9. ...         
  10. word[ 3 ]    l
  11. word[ 2 ]    l
  12. word[ 0 ]    h
  13. word[ 4 ]    o
  14. word[ 1 ]    e
  15. >>> 
Sep 19 '07 #2
Simple question here. Im trying to figureout how to write a program in such a way that when a word is entered, the output is each individual letter of that word in a random order. so if pizza is entered, the output would be
P
Z
A
I
Z

I have a general idea of how to output the letters, but not without them repeating. Id like to figure this out myself, but would appreciate being pointed in the right direction. heres the code thus far:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. word = "index"
  4. print "The word is: ", word, "\n"
  5.  
  6. high = len(word)
  7. low = -len(word)
  8. x=len(word)
  9.  
  10.  
  11. for i in range(x):
  12.     position = random.randrange(low, high)
  13.     print "word[", position, "]\t", word[position]
  14.  
  15. raw_input("\n\nPress the enter key to exit.")
  16.  
thanks
Try using 0 as your low bound. When you have a negative low bound and your program randomly picks a negative index, it will look backwards through the word which you don't need to do.
Also, every time you find a random letter, remove it from the word so you don't pick it again. Remember to decrease you high bound by one every time you remove a letter since the word will be shorter
You could even go without bounds at all and use a statement something like:
Expand|Select|Wrap|Line Numbers
  1. position = random.randrange(len(word))
  2.  
That way it will automatically adjust to the length of your word, and the lower bound will be 0 by default.
Sep 19 '07 #3
bartonc
6,596 Expert 4TB
Try using 0 as your low bound. When you have a negative low bound and your program randomly picks a negative index, it will look backwards through the word which you don't need to do.
Also, every time you find a random letter, remove it from the word so you don't pick it again. Remember to decrease you high bound by one every time you remove a letter since the word will be shorter
You could even go without bounds at all and use a statement something like:
Expand|Select|Wrap|Line Numbers
  1. position = random.randrange(len(word))
  2.  
That way it will automatically adjust to the length of your word, and the lower bound will be 0 by default.
Except that random.randrange() returns an iterator and this one needs an int.
Sep 19 '07 #4
Except that random.randrange() returns an iterator and this one needs an int.
Maybe it's an accident, but this works:
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> word = "index"
  3. >>> while len(word):
  4. ...        position = random.randrange(len(word))
  5. ...        print word[position]
  6. ...        word = word.replace(word[position], "", 1)
  7. ...
  8. e
  9. d
  10. i
  11. n
  12. x
  13. >>>
I use 2.5 if that matters
Sep 19 '07 #5
bartonc
6,596 Expert 4TB
Maybe it's an accident, but this works:
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> word = "index"
  3. >>> while len(word):
  4. ...        position = random.randrange(len(word))
  5. ...        print word[position]
  6. ...        word = word.replace(word[position], "", 1)
  7. ...
  8. e
  9. d
  10. i
  11. n
  12. x
  13. >>>
I use 2.5 if that matters
Well, I'll be... You taught me something, there. Thank you. (2.5 doesn't matter)
Sep 19 '07 #6
Heres where it gets complicated. This is a "brain teaser" for a class i am taking. The trick here is that i cannot use a while loop and i must use the negative range as well as positive.
Sep 19 '07 #7
Heres where it gets complicated. This is a "brain teaser" for a class i am taking. The trick here is that i cannot use a while loop and i must use the negative range as well as positive.
It sounds like recursion is a good way to go here. It's very handy when teachers say you can't use a specific kind of loop.
Expand|Select|Wrap|Line Numbers
  1. def recurse(word):
  2.     if word == "":# base case
  3.         return
  4.     else:
  5.         position = random.randint(-len(word), len(word)-1)
  6.         print word[position]
  7.         recurse(word.replace(word[position], "", 1))# call the function again with the printed letter missing from the word
  8.  
Sep 19 '07 #8
bvdet
2,851 Expert Mod 2GB
Why not use random.sample()?
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def randWord(word):
  4.     for letter in random.sample(word, len(word)):
  5.         print letter
Sep 19 '07 #9
This is starting to drive me crazy. I dont see how this can be accomplished without using a while loop. We havent really covered functions in this class as of yet, so there must be another way...without getting fancy.

Thanks for all the suggestions thus far.
Sep 19 '07 #10
bartonc
6,596 Expert 4TB
This is starting to drive me crazy. I dont see how this can be accomplished without using a while loop. We havent really covered functions in this class as of yet, so there must be another way...without getting fancy.

Thanks for all the suggestions thus far.
OK. Here's a trick (thanks bvdet for your excellent use of the random module!):
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. word = "index"
  4.  
  5. for letter in random.sample(word, len(word)):
  6.     print letter
Sep 19 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
19
by: Johannes Nix | last post by:
Hello, yesterday I met a cute person (after my dance class) who told me about an interesting experiment regarding cognition. People were told to read a typed text; However, in every word in the...
4
by: anita | last post by:
I had posted this question before, but did not hear from anybody. Can somebody pls help me out. I am creating a table with two fields F1, F2 and F2 has about 50,000 randomly generated alphanumeric...
7
by: MattB | last post by:
I have the following code to generate random passwords for new users of an application. Const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstyvwxyz1234567890" Dim r As Int16, i As...
2
hpbutterbeer
by: hpbutterbeer | last post by:
I'd like to get a word from a text file and print the letters of the word at random in the screen output. How do i do this? thanks for tips.
14
by: avanti | last post by:
Hi, I need to generate random alphanumeric password strings for the users in my application using Javascript. Are there any links that will have pointers on the same? Thanks, Avanti
1
by: Jeff | last post by:
hey gang. I have a code to create a random string of letters. The number of them can be whatever I desire. what i would like to do, is have it both letters and integers. how would i modify...
2
by: aboxylica | last post by:
I have a code in which i create a set of alphabets of specified count .Now in particular positions in it i want to embed my choice of letters how do i do it.here is my code def random_seq(): ...
3
by: miraan | last post by:
Hi, Right now I am working on creating a string of 5 random letters. I have already created the script, it does create a unique random letter but it repeats it 5 times. Here is what I have so far: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.