Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Can i prevent the random generator to generate already used numbers?
#1
Hi all!

I'm new to Python and played around a bit with the random module a little as a part of my learning process.
I wanted to do i lottery game that randomly spits out 7 numbers in the range of 1 to 34 and puts them in a list.

The problem is that the random generator will generate the same number more than once. I was able to solve this by generating a new set of numbers if doubles were found. But what i would prefer to do is preventing the random generator to generate already used numbers, as it would in a real lottery game. The used numbers should be excluded from the random generator so to speak. Is this possible to do?

Thanks a lot for your help!

                for x in range(0, 7):
                        lottorad[x] = random.randint(1, 34)
Quote
#2
Look at random.sample()
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
If you don't want the outputs to repeat
, you can make a list and a if statement to check if there such output in the list. Here's the code:
outputs = []
for x in range(7):
        lottorad[x] = random.randint(1, 34)
        if lottorad[x] in outputs:
                lottorad[x] = random.randint(1, 34)
        else:
                outputs.append(lottorad[x])

Sorry, if the script doesn't work. I just keep entering 'lottorad[x]' like a name of a variable
Quote
#4
Thanks a lot all for your help, random.sample did the trick.


My code went from this:
import random

lottorad = [0, 0, 0, 0, 0, 0, 0]
unik_rad = False



while unik_rad == False:
        for x in range(0, 7):
                lottorad[x] = random.randint(1, 34)
        # lottoraden är nu fylld med 7 nummer.



        # kontrollerar om samtliga nummer i lottoraden är unika
        for x in range(1, 35):
                lika = lottorad.count(x)
                if lika > 1: # minst två lika nummer har hittats
                        unik_rad = False
                        break
                else:
                        unik_rad = True


lottorad.sort()
print(lottorad)
to this:
import random

lottorad = []
lottorad = random.sample(range(1, 34), 7)
lottorad.sort()
print(lottorad)
Thanks again, problem solved Smile
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to prevent python from going to new line in for loop? idknuttin 2 101 Jan-15-2020, 04:27 PM
Last Post: jefsummers
  output a list of random numbers 'x' columns wide adityavpratap 4 112 Jan-13-2020, 05:32 PM
Last Post: perfringo
  generate random variables based on a non-standard t-distribution nathalie 4 202 Dec-03-2019, 12:11 AM
Last Post: scidam
  Random Password Generator with Weird Output. Why? Selenestica 2 276 Sep-11-2019, 04:36 AM
Last Post: Selenestica
  Working with Random Generated Numbers YoungGrassHopper 4 307 Sep-10-2019, 06:53 AM
Last Post: YoungGrassHopper
  Need to generate random numbers Gateux 8 437 Jul-19-2019, 03:37 PM
Last Post: Man_from_India
  Generate unique random numbers from different lists Takeshio 5 481 May-24-2019, 07:29 PM
Last Post: ichabod801
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 381 May-09-2019, 12:19 PM
Last Post: Pleiades
  How can I prevent context switching when calling an async function? AlekseyPython 1 526 Feb-20-2019, 10:37 AM
Last Post: AlekseyPython
  3 random numbers MrGoat 1 397 Jan-15-2019, 06:33 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)