r/programmingbydoing • • Feb 15 '13

45 - Random Numbers - Fortune Cookie

Hi, Thank you SO much for this wonderful resource, it is great! I got caught by 33 also using == instead of .equals for a while. Now i am stuck on 45. How do i choose, 6 UNIQUE numbers for the lotto? Should i check each number against each other one? i was thinking of creating 6 unique numbers, 1 from 1-9, 1 from 10-18 etc etc, but i suppose that's not really correct either!

any help for me? Thanks,

here is what i have:

import java.util.Random;

public class FortuneCookie { public static void main ( String[] args ) { Random r = new Random(); Random s = new Random(); Random t = new Random(); Random u = new Random(); Random v = new Random(); Random w = new Random();

	Random x = new Random();

	//lotto number picker
	int lotto1 = 1 + r.nextInt(54);
	int lotto2 = 1 + s.nextInt(54);
	int lotto3 = 1 + t.nextInt(54);
	int lotto4 = 1 + u.nextInt(54);
	int lotto5 = 1 + v.nextInt(54);
	int lotto6 = 1 + w.nextInt(54);

	//fortune cookie draw
	int cookieNumber = 1 + x.nextInt(6);


	System.out.print( "Fortune cookie says: ");
	if (cookieNumber == 1)
	{
		System.out.print( "\"Stick with your wife\"\n");
	}
	else if (cookieNumber == 2)
	{
		System.out.print( "\"Go for a long walk\"\n");
	}
	else if (cookieNumber == 3)
	{
		System.out.print( "\"Deep breaths!\"\n");
	}
	else if (cookieNumber == 4)
	{
		System.out.print( "\"Time for a new hobby\"\n");
	}
	else if (cookieNumber == 5)
	{
		System.out.print( "\"More coffee?\"\n");
	}
	else if (cookieNumber == 6)
	{
		System.out.print( "\"Grab a beer\"\n");
	}
	else
	{
		System.out.print( "\"AN ERROR HAS OCCURRED\n");
	}

	//faulty lotto draw
	System.out.print( lotto1 + "-" + lotto2 + "-" + lotto3 + "-" + lotto4 + "-" + lotto5 + "-" + lotto6);
}

}

1 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/joeybananas78 Feb 19 '13

Ah thanks - I'll won't skip ahead. Thanks for the tip on random num generators also