# Week 04 Lab Tutorial - Lotto Game

4 posts / 0 new
orourb25
Week 04 Lab Tutorial - Lotto Game

Hi

I'm having a little difficulty on the Lotto exercise from the week 4 Lab. I'm  trying to write the code to check the uniqueness of each of the numbers generated but I'm having difficulty writing it.

Does anyone have some tips/hints?

Thanks!

Barbara

lawlom32
Week 04 Lab Tutorial - Lotto Game

Hi Barbara

Probably a bit late but I only got around it myself now.

Try to put a while loop in after you have generated you 2nd to 6th numbers.

If your random = r, first int = a and second int = b, the while loop for the 2nd number should basically look like:

while(b == a)

int b = r.nextInt(46) +1;

The +1 gets the programme to generate a number between 1 and 46.

Then move on to the 3rd number and so on.

The problem I had after this was that I had to add additional while loops for each previous number generated as I couldn't get a while loop working for more than one int at a time.

If anyone has any ideas on that I would be grateful!

Thanks

Mike

kaghavan
Hi, I wonder what is your

Hi,

If you dont get how the random numbers are generated, then its easy,

A part of the code where you write

Random r= new Random(17);

// this is the class random class which is in built and is called by the java program. It declares an object "r" which is instantiated using new keyword. Then memory is allocated for the "r" variable in the system. The number 17 in curve brackets corresponds to mentioning the random variable to return a random number between 0 and 17.

I hope this helped, If you have more doubts let us know

rgrds

Karthika

orourb25
Hi Karthika Many thanks for

Hi Karthika

Apologies! My initial post was not very clear.  In the LottoGame exercise we have to generate 6 random numbers. The approach I'm taking (though at present I feel it's very much incorrect):

//System generates 1st random number

int num1 = r.nextInt();

//System generates 2nd random number

int num2 = r.nextInt();

//If 2nd random number is equal to 1st random number, system to create new 2nd number

if(num2==num1)

{

int num2 = r.nextInt();

}

and this continues on to the 6th number generated and checked.

I've compiled what I've written and it generates a lot of errors making me think I've taken the wrong approach overall. I would very much appreciate hints on how I should approach this exercise.

Thanks again,

Babrara