Python Forum
RNG; One variable must not be equal to another - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: RNG; One variable must not be equal to another (/thread-13297.html)

Pages: 1 2


RNG; One variable must not be equal to another - DavidRobinsons - Oct-09-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.


RE: RNG; One variable must not be equal to another - buran - Oct-09-2018

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.


RE: RNG; One variable must not be equal to another - DavidRobinsons - Oct-09-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.


RE: RNG; One variable must not be equal to another - micseydel - Oct-09-2018

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).


RE: RNG; One variable must not be equal to another - ichabod801 - Oct-09-2018

1 to 42 with no repeats? Sounds more like a lottery number.


RE: RNG; One variable must not be equal to another - DavidRobinsons - Oct-09-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 Smile


RE: RNG; One variable must not be equal to another - DavidRobinsons - Oct-10-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.


RE: RNG; One variable must not be equal to another - micseydel - Oct-10-2018

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'



RE: RNG; One variable must not be equal to another - buran - Oct-10-2018

@micseydel is right that it wouldn't scale uf using itertool.combinations. Didn't think of it... I like the idea for using shuffle()


RE: RNG; One variable must not be equal to another - ichabod801 - Oct-10-2018

It's simpler with random, since random comes with Python.

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