Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random.randrange
#1
I need to use the random.randrange function to randomly select a character from a string the string is the alphabet. I cant use random choice
Reply
#2
import string

print(string.ascii_letters)
# index access
print(string.ascii_letters[0])
n_letters = len(string.ascii_letters)
index = random.randrange(0, n_letters + 1)
# read the documentation about randint and randrange to understand the difference
I think you'll find out the rest.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
import random
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz"
random_characters = len(alphabet)
index = random.randrange(0, random_characters + 1)
print(index)

I'm a bit confused. I have to use the variable alphabet(I'm new to python 1 week in)
Reply
#4
Maybe I am missing something but from documentation: random.randrange

Quote:random.randrange(stop)
/../
Return a randomly selected element from range(start, stop, step).

Why random.randrange(0, random_characters + 1) and not justrandom.randrange(len(alphabet)). Latter will give random number which is in range of valid indexes:

>>> a = [1, 2, 3]
>>> len(a)
2
>>> max(random.randrange(0, len(a) + 1) for x in range(100))
3
>>> max(random.randrange(len(a)) for x in range(100))
2
>>> a[3]
Index Error
/../
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
i need the specfic letter for example 0 will be A.
I need the output to show A
Reply
#6
I think that I do understand. Just if you have random.randrange(0, len(alphabet) + 1) then:

>>> alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz"
>>> len(alphabet)
51
>>> max(random.randrange(0, len(alphabet) + 1) for x in range(1000))
51
>>> alphabet[51]
Index Error
/../
IndexError: string index out of range
This means that random.randrange can return you index which is out of range.

Regarding conditions: must all letters be unique or can there be duplicates?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
there can be duplicates
unfortunately i cant use the for or in functions as it is homework.
Reply
#8
Don't make your own alphabet string.
You have forgotten the n.

import string
print(string.ascii_letters)
The IndexError is raised, because you try to access the 51th element, but the indexing starts with 0.
This means, if you have 51 elements/letters, the last letter in this list is at index 50. The first letter is at index 0.

You need to learn more about slicing: https://stackoverflow.com/questions/5092...e-notation

Read the documentation about randrange and randint.
For randrange you can use the output from len. With randint the stop value is inclusive. This means it is len(your_strig) + 1.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
Your nearly there in your code post #3
Just add alphabet[index]
import random

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz"
random_characters = len(alphabet)
index = random.randrange(0, random_characters + 1)
print(index)
print(alphabet[index])
A little fancier output Wink
import random

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz"
random_characters = len(alphabet)
index = random.randrange(0, random_characters + 1)
print(f'Random characters at index <{index}> give output <{alphabet[index]}>')
Output:
Random characters at index <3> has result of <D> Random characters at index <1> give output <B> Random characters at index <24> give output <Y>
Reply
#10
thanks for the help guys relatively new to this. Just asking how would I ask for more than one value say I
wanted 6 different random letters (there can be a duplicate)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,508 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Remove space after random.randrange(1, 11) rs74 4 2,317 Jun-06-2020, 07:46 AM
Last Post: rs74

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020