Python Forum
Can i prevent the random generator to generate already used numbers?
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)
Reply
#2
Look at random.sample()
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#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
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  random numbers, randint janeik 2 526 Nov-27-2023, 05:17 PM
Last Post: janeik
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 701 Aug-09-2023, 05:51 PM
Last Post: Calab
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,012 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  prime numbers with iterator and generator cametan_001 8 1,771 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  List of random numbers astral_travel 17 2,531 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,428 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Generate random id (please help!) Kjaglewicz 8 1,877 Aug-29-2022, 09:37 AM
Last Post: fracjackmac
Smile How we can prevent screen recording murad_ali 3 1,757 Jul-29-2022, 10:29 AM
Last Post: DeaD_EyE
  How to prevent python from going to new line in for loop? idknuttin 3 4,836 Feb-11-2022, 05:40 AM
Last Post: deanhystad
  Random coordinate generator speed improvement saidc 0 2,021 Aug-01-2021, 11:09 PM
Last Post: saidc

Forum Jump:

User Panel Messages

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