2

I'm trying to write an encryption program that generates 8 different random numbers and converts them to ASCII. If possible I'd like to use the 'random' function in python, but welcome other help.

So far my code for generating the numbers is to assign a value to a different run of the random.randint() function 8 different times, the problem is that this is sloppy. A friend said to use random.sample(33, 126, 8) but I can't get this to work.

Any help is extremely welcome.

3
  • Should / shouldn't values be unique? Commented May 10, 2015 at 17:06
  • As long as the values are not all the same it doesn't matter, they should have been independently generated Commented May 10, 2015 at 17:08
  • "The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes." Commented May 10, 2015 at 17:13

4 Answers 4

6

You can pass xrange with your upper and lower bound to sample:

from random import sample

print(sample(xrange(33, 126),8))

An example output:

[49, 107, 83, 44, 34, 84, 111, 69]

Or range for python3:

 print(sample(range(33, 126),8))

Example output:

 [72, 70, 76, 85, 71, 116, 95, 96]

That will give you unique numbers.

If you want 8 variables:

a, b, c, d, e, f, g, h =  sample(range(33, 126), 8)

If you want ascii you can map(chr..):

from random import sample

print map(chr,sample(xrange(33, 126), 8))

Example output:

['Z', 'i', 'v', '$', ')', 'V', 'h', 'q']
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks very much! just quickly, how would I then split them individually and assign them to be independent variables?
just unpack, you want 8 variables yes?
Yes, sorry, forgot about unpacking!
@Matt123 Though unpacking is the right way, Why don't you just use the list as such and the elements using list indexes, i.e l = sample(range(33, 126),8) and l[0], l[1], etc .. ?
Thank you, that is a good idea, certainly more efficient for creating variables
|
2

Using random.sample is technically not what you want - the values are not independent since after you choose the first number (from 93 options) you only have 92 options for the second number and so on.

If you're ok with that you can use Padraic's answer.

If n (in your case n = 8) is much smaller than N (in your case N = 126-33 = 93) this should be fine, but the correct answer will be

a, b, c, d, e, f, g, h = [random.randint(93, 126) for _ in xrange(8)]

edit: More importantly, if you decide to increase n to a state where n > N, you'll get a ValueError

1 Comment

Thank you, I will probably use this if it does truly give random numbers.
1

If your goal is to pick 8 random ASCII characters in the range [33..126], you can do that directly. First, ASCII characters in that range is called string.printable. You can use the random.sample function to pick out 8 from there:

import string
import random

result = random.sample(string.printable, 8)

result is now a list of 8 random printable characters.

1 Comment

That is not the same as xrange(33, 126)), the range for printable is 9,10,11,12,13,32-126
1

I recommend using a generator.

from random import randint

def randnums(number, startnum=0, endnum=10):
    for i in range(number):
        yield randint(startnum, endnum)

print(list(randnums(8, endnum=100)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.