Lottery number generator – Java Example

Lottery number generator – Java Example


Hello everybody, welcome to the Java
example section. In this section, I’m going to a set up a lottery number
generator, and my idea is like this. I would like to pick six numbers between 1
and 49. And during the random draw, it is possible that the numbers being drawn has already appeared beforehand. So after
drawing a certain number, I’d like to check if it is already repeated in the drawn
numbers. If it is the case, I would loop the draw until it is not repeated.
Finally I would … I would sort numbers by using the Arrays.sort method and I would
present all the numbers drawn finally Okay, let us see the implementation. Since
I’m going to generate some random numbers, I need to import the ran … the
random library beforehand. Ok, and then I can go to the main method.
I need a random number generator, so I need to create a Random object. Ok, a
Random object has a reference variable called r-a-n-d, rand Okay um the smallest number is one, the
largest number is 49. And I would like to have six numbers, so I would have six
slots. Here I would set up an array of in … integers called lotteryArray. And of course there are six elements in this array. And when I set
the arrays in this way, all the elements in this array are already initialized to
be zeros. And then I need to check for repeated numbers. I’m going to remember
the result of having repetition by using a boolean variable. The variable is called
isRepeated. And I would like to remember the random number provided by the Random
object, so I need a variable called randomNumber, which is an integer
primitive. So now I’m going to do the random draw Here, the counter is called um indexDrawn.
That means it traces the index of the number that is being drawn. Since
I have to make a draw at least once, I can simply use a do-while loop. I would say
that the number is not repeated in the first place. So if it is really the case,
I will set the isRepeated flag to be true later Okay the random number is
provided by the Random object called “rand”. And the method to use is called
nextInt. Here I simply have 49 as the actual para … parameter of the nextInt
method. And after picking up the number by using the nextInt method with 49
being the actual parameter, the number provided by the nextInt method can be
simply between 0 and 48. But I want it to be to be between 1 and 49. So I
need to add the minimum number which is one to the final answer coming from the
nextInt method. So in this case, the number provided by the nextInt method
together with the addition of “min” variable will be equal to
a value between one and 49, okay Here I’m going to check for the repeated
numbers for my upcoming draws My idea is like this. I would like to check for the
repeated number against the filled nonzero elements. Why should I need nonzero
elements? Because the numbers to be drawn would be between 1 and 49. And since
all the elements in the array has been … have been initialized as zeros, I simply
check for the nonzero numbers to represent that the number has already
been put by the Random object called “rand”, okay Okay I need to check for the
repeated number against the filled non-zero element. And the nonzero
elements would end with index called indexDrawn which is simply the counter
for the outer for loop, okay Okay when I do the checking in the inner
for loop, I hope that none of the drawn numbers would repeat the random number
provided but nextInt method If any of the drawn number is equal to the random
number, I need to do the random draw again. How can I do that? I can do
something like this. I can set the isRepeated flag to be true, and then I make
a breaking statement. That means if any of the drawn number is equal to the
random number, I need to tell the computer that, oh there is a repeated
number, and I would need to leave the for loop, okay, which is an outer …, sorry
which is an inner for loop Okay and then I would go back to the
nextInt method again to give me another random number. So if this random number
is already present in my previous draws, I need to say that isRepeated is
true. And then I will leave the inner for loop to do the random draw again.
So um the do-while loop is like that In order to complete the statement, I need to
say something like here While isRepeated becomes true, I need to go over
the random draw process and the checking process process against the drawn
numbers stored in the lotteryArray, okay. If there is no repetition, I don’t
need to go over the nextInt method again and again and again because the isRepeated
variable is false. If it is false, I don’t need to generate the
numbers repeatedly until there is no match against my previous drawn numbers.
If it is the case, I simply go to the outer for loop to repeat the random
draws for all my remaining numbers, okay If I’m so lucky that there is no need
to do further repetition, I simply store the random number into the lottery array,
okay. And the index is called indexDrawn That means I can leave the do-while loop to put the number that is a non-repeated number into the lottery array.
So that finishes the random draw, which makes sure that no number will be
repeated in the lottery array And then I can do the sorting of the numbers in the
lotteryArray for better presentation I give a prompt first. Now I print out
the numbers one by one which follows the sorted order Okay let me save it and run
it Oh there’s an error, let me check.
Well, it has to be Arrays, not Array, okay So let me try to run it again. I got the
error. I need to import the Arrays library before I can use the sort method So let me save it and run it. Still an
error, where is it? randomNumber not randonNumber, okay. Save it, run it. Yes, I
can get the six numbers as follows, all of which are non-repeated values. Why
not repeated? Because I have set up a do- while loop to check for the repeated numbers
against the numbers that I have drawn beforehand. If there is a repeated number
in the lottery array, I need to redraw the number until there is no match
against the drawn numbers stored in the lottery array. So let me run it again.
I would have another set of numbers drawn, okay, all of which are non-
repeated numbers. Run it again. Okay I would get another set of numbers. So this is
the major idea of the lottery number generator, which ensures that not …
there … there is not repeated number in the final answer stored in the lottery
array. And the other is like this. I simply use the do-while loop to make
sure that um I need further random draws until I have no match against the
lottery number drawn already. My code is intuitive and straightforward. However, it
it may not be the best or the most efficient approach to the problem. If you
can think about any idea to improve my code, please and do so on the comment
section below the video. If you like this video, please give me a like and please
subscribe to my channel. Thank you for watching

1 thought on “Lottery number generator – Java Example”

Leave a Reply

Your email address will not be published. Required fields are marked *