Python Forum

Full Version: RNG; One variable must not be equal to another
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I am still studying about importing modules, and suddenly I came with an idea of a random number generator which will generate a six-number combination out of numbers within a specified range. I am going to insert a specific portion of my codes

from random import *

die1 = randint(1,42)
die2 = randint(1,42) 
die3 = randint(1,42)
die4 = randint(1,42)
die5 = randint(1,42)
die6 = randint(1,42)

print("For 6/42: ", die1, "-", die2, "-", die3, "-", die4, "-", die5, "-", die6)
I want to make the resulting numbers non-repeating, which means if die1 picked 5, the other variables must not pick 5. That goes for the other variables as well. How will I make it? The book I am using as reference haven't mentioned how to do so.
first of all repetitive code like above is really poor idea and should be avoided.
check itertools.combinations() and use random.choice() to get randomly one combination
more naive approach would be to keep a list of numbers already drawn and keep drawing random numbers until you have 6 distinct numbers. here you may also use set.
I know that repetitive codes is a bad idea. However I just started learning Python few days ago on a casual basis, hence I still don't know a lot of possible codes for this language, forcing me to repeat some lines of codes.
Using combinations() here is clever but wouldn't scale. What you want is "sampling without replacement" and the easiest way to do that would be with a numpy function which allows you to set replacement to False. Apparently Python's random module doesn't have this built-in, which is somewhat unfortunate.

If you don't mind adding a dependency, I'd use numpy. Otherwise the random number generation and set checking is probably what I would do. As a third option, you could consider using the built-in random.shuffle() one time and then popping (this would be less efficient than set-checking, but more efficient than combinations).
1 to 42 with no repeats? Sounds more like a lottery number.
(Oct-09-2018, 10:49 PM)ichabod801 Wrote: [ -> ]1 to 42 with no repeats? Sounds more like a lottery number.

Yep, it is. Just for fun thing Smile
(Oct-09-2018, 10:31 PM)micseydel Wrote: [ -> ]Using combinations() here is clever but wouldn't scale. What you want is "sampling without replacement" and the easiest way to do that would be with a numpy function which allows you to set replacement to False. Apparently Python's random module doesn't have this built-in, which is somewhat unfortunate.

If you don't mind adding a dependency, I'd use numpy. Otherwise the random number generation and set checking is probably what I would do. As a third option, you could consider using the built-in random.shuffle() one time and then popping (this would be less efficient than set-checking, but more efficient than combinations).

I am starting to think last night that the thing I want to happen is beyond me for now, and I am starting to give up. But thanks for suggesting me three options, I'll look to them.
With numpy, it's very simple
>>> import numpy
>>> '-'.join(map(str, numpy.random.choice(range(1, 43), 6, replace=False)))
'13-36-12-7-25-31'
@micseydel is right that it wouldn't scale uf using itertool.combinations. Didn't think of it... I like the idea for using shuffle()
It's simpler with random, since random comes with Python.

import random
balls = '-'.join(map(str, random.sample(range(1, 43), 6)))
Pages: 1 2