Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a random letter generator with a range?

Tags:

python

I was wondering if there is a random letter generator in Python that takes a range as parameter? For example, if I wanted a range between A and D? I know you can use this as a generator:

import random
import string

random.choice(string.ascii_letters)

But it doesn't allow you to supply it a range.

like image 908
javanewbie Avatar asked Aug 19 '16 15:08

javanewbie


People also ask

How do you generate random letters?

All you need to do is select the number of different random letters your want generated, what language alphabet you want and then if you want upper, lower or both cases displayed. Once this is done, all you need to do is hit the "Generate Random Letter" button and your random letters will appear.

Is there a random letter generator?

This is a random letter generator that picks a random alphabet by using a wheel. The letter generator has several input options and text-transformation options. The letters involved are the 26 alphabets from A to Z.

Can Excel generate random letters?

Add the CHAR function to create a character. Add the randbetween function to make the character random. Add 65, 90 to make it a letter from the alphabet. Press Enter to complete the formula: =CHAR(randbetween(65,90)).


3 Answers

You can slice string.ascii_letters:

random.choice(string.ascii_letters[0:4])
like image 103
Cormac O'Brien Avatar answered Nov 15 '22 21:11

Cormac O'Brien


>>> random.choice('ABCD')
'C'

Or if it's a larger range so you don't want to type them all out:

>>> chr(random.randint(ord('I'), ord('Q')))
'O'
like image 37
Stefan Pochmann Avatar answered Nov 15 '22 23:11

Stefan Pochmann


Ascii is represented with numbers, so you can random a number in the range that you prefer and then cast it to char.

like image 40
user3435469 Avatar answered Nov 15 '22 22:11

user3435469