Posts: 6
Threads: 3
Joined: Oct 2018
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.
Posts: 8,090
Threads: 154
Joined: Sep 2016
Oct-09-2018, 07:11 AM
(This post was last modified: Oct-09-2018, 07:11 AM by buran.)
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.
Posts: 6
Threads: 3
Joined: Oct 2018
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.
Posts: 2,344
Threads: 62
Joined: Sep 2016
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).
Posts: 4,229
Threads: 97
Joined: Sep 2016
1 to 42 with no repeats? Sounds more like a lottery number.
Posts: 6
Threads: 3
Joined: Oct 2018
(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
Posts: 6
Threads: 3
Joined: Oct 2018
(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.
Posts: 2,344
Threads: 62
Joined: Sep 2016
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'
Posts: 8,090
Threads: 154
Joined: Sep 2016
@ micseydel is right that it wouldn't scale uf using itertool.combinations. Didn't think of it... I like the idea for using shuffle()
Posts: 4,229
Threads: 97
Joined: Sep 2016
It's simpler with random, since random comes with Python.
import random
balls = '-'.join(map(str, random.sample(range(1, 43), 6)))
|