Results 1 to 3 of 3
Thread: Probably Really Noob Question
- 05-16-2011, 01:32 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
Probably Really Noob Question
Okay, so I'm not really THAT noobish at Java, but right now I really feel like it. This has to be some really stupid misconception or something.
Okay, so first of all:
Returns either 0 or 2, always. Because 0*2 = 0 and 1*2 = 2.Java Code:Random random = new Random(); for(int i=0; i<10; i++){ System.out.print(random.nextInt(2)*2); }
But then when I use it in a for-loop:
It acts as though it returns either 0, 1, or 2. I feel like it has to do maybe with the lack of the "1" possibility in the for-loop. Quite confused indeed. Why is it doing this?Java Code:Random random = new Random(); for(int i=0; i<10; i++){ for(int j=0; j<random.nextInt(2)*2; j++) System.out.print("1"); System.out.println(); }
- 05-16-2011, 01:38 AM #2
Using that in the condition of a loop is a really bad idea. Everytime it goes around the loop it will generate a different number so you can never guarantee how many times the loop will iterate.Java Code:random.nextInt(2)*2
If you change the code to that you should see more consistent results.Java Code:Random random = new Random(); for(int i=0; i<10; i++){ int end = random.nextInt(2)*2; for(int j=0; j<end; j++) { System.out.print("1"); } System.out.println(); }
- 05-16-2011, 01:44 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Noob question about using ints in booleans
By elfdreaming in forum New To JavaReplies: 6Last Post: 03-14-2011, 11:07 AM -
Reallyy noob question
By x2robbie2x in forum New To JavaReplies: 3Last Post: 02-18-2010, 04:58 AM -
Noob button question.
By dudejonne in forum Java AppletsReplies: 4Last Post: 10-20-2009, 10:51 PM -
Question - I'm a noob!
By Insaeno in forum New To JavaReplies: 5Last Post: 08-04-2008, 03:20 AM -
Noob question- easy
By mattonitto in forum New To JavaReplies: 7Last Post: 06-13-2008, 12:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks