Python Forum
RNG; One variable must not be equal to another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RNG; One variable must not be equal to another
#1
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.
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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).
Reply
#5
1 to 42 with no repeats? Sounds more like a lottery number.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(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
Reply
#7
(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.
Reply
#8
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'
Reply
#9
@micseydel is right that it wouldn't scale uf using itertool.combinations. Didn't think of it... I like the idea for using shuffle()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
It's simpler with random, since random comes with Python.

import random
balls = '-'.join(map(str, random.sample(range(1, 43), 6)))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020