Here's a programming challenge for you - A Lottery Picker. 1) Must not repeat the same number in one pick. 2) Picks cannot be identical in a batch of games. 3) User must control what the max. number will be, 1=< is not valid. 4) User must control how many numbers per game.4< is not valid. 5) User must control how many games per run, 1< is not valid. 6) Extra points if you put in a Special Number as most lotteries have. Good Luck !

(32) Comments

As I understand from your question, you want to create a Lottery Picker program with the following features: it should ensure that each number is unique within a game and that no two games are the same. Users should be able to set the maximum number allowed in the game (greater than 1), choose how many numbers are picked per game (more than 4), and decide how many games are generated in one run (more than 1). Additionally, the program should include a special number in each game. Okay but before I solve anything, I will get a pen and paper, approach your problem as if it were the most difficult problem in the world, and start analyzing it to determine what needs to be done. Personally, I wrote the following: 1. I have to ensure the user input is within valid ranges to prevent errors. 2. I have to use a set helps avoid duplicate games automatically. 3. I must ensure each game is unique and includes a special number. 4. I have to display the results shows the generated games clearly. Now, I will remember the face of my teacher in "Introduction to computer programming" subject, and will write the algorithm using pseudocode: ```vbnet START FUNCTION lottery_picker(max_number, numbers_per_game, games_per_run): // Validate inputs IF max_number <= 1 THEN RAISE ValueError("Max number must be greater than 1") END IF IF numbers_per_game <= 4 THEN RAISE ValueError("Numbers per game must be greater than 4") END IF IF games_per_run <= 1 THEN RAISE ValueError("Games per run must be greater than 1") END IF IF numbers_per_game >= max_number THEN RAISE ValueError("Numbers per game must be less than the maximum number") END IF INITIALIZE all_games AS an empty set WHILE length of all_games < games_per_run: PICK game AS a random sample of numbers_per_game from range 1 to max_number FIND remaining_numbers AS numbers from 1 to max_number not in game IF remaining_numbers is empty THEN RAISE ValueError("Not enough numbers left to choose a special number") END IF CHOOSE special_number from remaining_numbers APPEND special_number to game CONVERT game to a sorted tuple and assign to game_tuple ADD game_tuple to all_games FOR each game in all_games: PRINT "Numbers:" followed by game without the last element, "Special Number:" followed by the last element of game END FUNCTION WHILE TRUE: TRY: GET max_number from user input GET numbers_per_game from user input GET games_per_run from user input IF max_number <= 1 OR numbers_per_game <= 4 OR games_per_run <= 1 THEN RAISE ValueError("Invalid input values. Ensure max_number > 1, numbers_per_game > 4, and games_per_run > 1.") END IF IF numbers_per_game >= max_number THEN RAISE ValueError("Numbers per game must be less than the maximum number.") END IF CALL lottery_picker(max_number, numbers_per_game, games_per_run) BREAK OUT OF THE LOOP EXCEPT ValueError AS e: PRINT e END WHILE END ``` I believe I am done problem solved let's try to use a tool like PHP, Python, Java etc. to write the solution. Today I will make a change and I will use Python because it's easier to understand: ```python import random def lottery_picker(max_number, numbers_per_game, games_per_run): # Validate inputs if max_number <= 1: raise ValueError("Max number must be greater than 1") # I wrote: Ensures max_number is greater than 1 if numbers_per_game <= 4: raise ValueError("Numbers per game must be greater than 4") # I wrote: Ensures numbers_per_game is greater than 4 if games_per_run <= 1: raise ValueError("Games per run must be greater than 1") # I wrote: Ensures games_per_run is greater than 1 if numbers_per_game >= max_number: raise ValueError("Numbers per game must be less than the maximum number") # I wrote: Ensures numbers_per_game is less than max_number all_games = set() # I added: Initializes a set to store unique games while len(all_games) < games_per_run: # I wrote: Loops until we have the desired number of unique games game = random.sample(range(1, max_number + 1), numbers_per_game) # I added: Picks random unique numbers for the game remaining_numbers = [num for num in range(1, max_number + 1) if num not in game] # I wrote: Finds remaining numbers for the special number if not remaining_numbers: raise ValueError("Not enough numbers left to choose a special number") # I wrote: Ensures there are remaining numbers to choose from special_number = random.choice(remaining_numbers) # I added: Chooses a special number from remaining numbers game.append(special_number) # I wrote: Appends the special number to the list of picked numbers game_tuple = tuple(sorted(game)) # I added: Converts the list to a sorted tuple to ensure uniqueness in the set all_games.add(game_tuple) # I wrote: Adds the tuple to the set of games for game in all_games: # I wrote: Iterates through the set of unique games print("Numbers:", game[:-1], "Special Number:", game[-1]) # I added: Prints the numbers and special number for each game # User input with validation while True: try: max_number = int(input("Enter the maximum number: ")) # I wrote: Prompts user for the maximum number numbers_per_game = int(input("Enter the number of numbers per game: ")) # I wrote: Prompts user for the number of numbers per game games_per_run = int(input("Enter the number of games per run: ")) # I wrote: Prompts user for the number of games per run # Check if the inputs are valid if max_number <= 1 or numbers_per_game <= 4 or games_per_run <= 1: raise ValueError("Invalid input values. Ensure max_number > 1, numbers_per_game > 4, and games_per_run > 1.") # I wrote: Checks if any input value is invalid if numbers_per_game >= max_number: raise ValueError("Numbers per game must be less than the maximum number.") # I added: Ensures numbers_per_game is less than max_number lottery_picker(max_number, numbers_per_game, games_per_run) # I wrote: Calls the function with user inputs break # I added: Exit the loop if inputs are valid except ValueError as e: print(e) # I wrote: Prints the error message if inputs are invalid ``` ![Python Code](https://i.ibb.co/JpJjxd0/python.png) ![Python Code](https://i.ibb.co/9NqdgH4/python2.png) You can implement the same concept using Java, PHP, C++, etc. Good Luck.

I very much hope that [Cyberat](https://chat-to.dev/profile?u=Cyberat) will see the answer and leave a comment saying that everything is in order. Well, in my opinion and from the tests I've carried out, the answer is correct.

I hope so

Cyberat, The "repeat" loops prevent duplicates.

Fschmidt, to be more clear, you build a program for a machine printing custom lottery picks on a screen. Any lottery, so the input is user input not preset numbers not yours not anyone else's. You must have an input and verification process of such.

as far as i can tell, i don't think anyone completed their task successfully! [@fschmidt's](https://chat-to.dev/profile?u=fschmidt) solution was the right one

Fschmidt, explain where and how, I don't see it at all.

Cyberat, My code prevents duplicates.

FSchmidt you did not follow the challenge, you followed Java Juggler's choice of 49, 6, 10. Must have USER INPUT, not your choice. Must validate input.

I can say Luan language has very weird For/Next loops.

Fschmidt, well done and complete code, but no Duplicate Checks. Must have duplicate check per game and per set of games. Best random(timer) will create duplicate numbers & duplicate games also. Maybe there is a language problem here let's say I have 5 numbers: 2, 34, 12, 44, 2. This pick is not desired. Also a game set should not repeat, same way. Run your program & test against it, run 10 games, run 20 games.

I'm in the middle of two programs and couldn't pay much attention to the code 🤔

>Took me about 3 days, take your time. Do run it and test it. Rule of thumb, run with 6 numbers per game and 10-12 games to look for duplicates. Don't trust the randomizer, correct it. `@Cyberat` said here 👆 that it took 3 days to solve and I did a few minutes and got some errors

JavaJuggler, Arrive at what result? My program? I read the requirements, looked at your code for variable names, and wrote my code. It took about 15 minutes.

I'm slowly starting to like this language, Mr. fschmidt. And by the way, how did you arrive at this result?

In my programming language [Luan](http://www.luan.software/): local Luan = require "luan:Luan.luan" local error = Luan.error local range = Luan.range or error() local ipairs = Luan.ipairs or error() local stringify = Luan.stringify or error() local Math = require "luan:Math.luan" local random = Math.random or error() local Table = require "luan:Table.luan" local sort = Table.sort or error() local concat = Table.concat or error() local Io = require "luan:Io.luan" local print = Io.print or error() local function lottery_picker( max_number, numbers_per_game, games_per_run ) max_number >= 1 or error "max_number must be at least 1" numbers_per_game >= 4 or error "numbers_per_game must be at least 4" max_number >= numbers_per_game or error "max_number must be at least numbers_per_game" games_per_run >= 1 or error "games_per_run must be at least 1" local games = {} local picks = {} for _ in range(1,games_per_run) do local game, pick repeat game = {} local used = {} for _ in range(1,numbers_per_game) do local number repeat number = random(1,max_number) until used[number] == nil used[number] = true game[#game+1] = number end sort(game) pick = concat( game, ":" ) until picks[pick] == nil picks[pick] = true games[#games+1] = game end return games end local games = lottery_picker( 49, 6, 10 ) print("Lottery Games:") for _, game in ipairs(games) do print( concat( game, " " ) ) end

Ok. thanks for the tip. as soon as I've finished I'll post it here in the comments.(y)

Took me about 3 days, take your time. Do run it and test it. Rule of thumb, run with 6 numbers per game and 10-12 games to look for duplicates. Don't trust the randomizer, correct it.

I'm trying to make something better organized and compressible. I'll post the result soon.

Hint, requires at least 3 nested loops for duplicate checks.

Input still faulty, since I hate Java and not that familiar, let me write your input in C# best I remember: Input ("What is the maximum number to have in a game ? :", max_number); While max_number =< 1 Print ("Invalid Number, must be greater than 1, try again."); Do Would need a set for each input.

Now you removed the check for duplicate numbers and have no check for duplicate games. This is the entire brain & core of the exercise.

I specified user picks amount of games and all that input, not programmer. Let me be clearer, I walk up to a machine that runs your program, the machine will ask me three questions: What is the max. number in the game, how many numbers to get for a game, how many games to get for this session. Runs the process and returns to main menu on key press. RUN YOUR OWN PROGRAM, test it, not just "I wrote this, how is it now ?".

see now `Mr Cyberat` ```py import random def lottery_picker(max_number, numbers_per_game, games_per_run): """ Generates a batch of lottery games with unique numbers and a bonus number. Parameters: max_number (int): The maximum number that can be picked. numbers_per_game (int): The number of unique numbers per game. games_per_run (int): The number of games per run. Returns: list: A list of games, where each game is a list of unique numbers with a bonus number. """ # Check if inputs are valid if not all(isinstance(param, int) for param in [max_number, numbers_per_game, games_per_run]) or max_number < 1 or numbers_per_game < 4 or games_per_run < 1: return "Invalid input. Please ensure all inputs are positive integers." # Initialize an empty list to store the games games = [] # Generate the games for _ in range(games_per_run): game = set() while len(game) < numbers_per_game: number = random.randint(1, max_number) game.add(number) games.append(sorted(list(game))) return games # Example usage games = lottery_picker(max_number=49, numbers_per_game=6, games_per_run=10) print("Lottery Games:") print("Number 1 Number 2 Number 3 Number 4 Number 5 Bonus Number") for game in games: print("{:8}".format(" ".join(map(str, game[:-1]))) + " " + str(game[-1])) ```

Make it simpler, list the last number as "bonus number", just a header text print marking the column.

The truth is that what sometimes seems simple becomes more complex in implementation. But I'm paying more attention and I'll get better results.

First of all put in the proper code for inputs and verification, you got it, just not in there. Also you don't want to invalidate ALL inputs for 1 wrong input, very annoying. Deprecate verification after each input.

Yes, it was a trick, sounds simple right, lottery picker ? I did it in C# and I ran into trouble seeing the results. Then I added the bonus as extra, when I solved it. I used Arrays and nested loops - hint.

the challenge is really complex and requires a great deal of thought. but i believe i've got to the bottom of the solution. if there's anything missing, let's try and come up with another solution.

I also asked for verification that games do not repeat in a batch. Yes, randomizer can do that in a set of 10+ games.

Does it run and perform ? I want the printed results. Cheap & easy way out, no text input or verification. Deprecate "Number not in game". It's almost like pseudo code. Nope you don't have it, incomplete work.

for this challenge I basically arrived at this result ```py import random def lottery_picker(max_number, numbers_per_game, games_per_run, special_number): """ Generates a batch of lottery games with unique numbers and a special number. Parameters: max_number (int): The maximum number that can be picked. numbers_per_game (int): The number of unique numbers per game. games_per_run (int): The number of games per run. special_number (int): The special number that must be included in every game. Returns: list: A list of games, where each game is a list of unique numbers including the special number. """ # Check if inputs are valid if max_number < 1 or numbers_per_game < 4 or games_per_run < 1 or special_number < 1 or special_number > max_number: return "Invalid input. Please ensure all inputs are positive integers and the special number is within the range." # Initialize an empty list to store the games games = [] # Generate the games for _ in range(games_per_run): game = set() while len(game) < numbers_per_game: number = random.randint(1, max_number) if number != special_number and number not in game: game.add(number) game.add(special_number) games.append(list(game)) return games # Example usage games = lottery_picker(max_number=49, numbers_per_game=6, games_per_run=10, special_number=7) for game in games: print(game) ``` This code defines a function lottery_picker that takes four arguments: max_number, numbers_per_game, games_per_run, and special_number. It generates a batch of lottery games with unique numbers and a special number, and returns a list of games. The function checks if the inputs are valid, and if not, it returns an error message. The example usage at the end of the code generates 10 games with 6 unique numbers each, where the maximum number is 49 and the special number is 7. the challenge has been launched, whoever has a better answer than mine, let's have it