1,105,340 Community Members

For Loop - every Nth number on new line using Random numbers

Member Avatar
musikluver4
Junior Poster in Training
82 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
//Date: 3/17/2010

import java.util.Random;

public class Bingo 
{
	public static void main(String[] args) 
	{
		Random bingo = new Random();
		
		for (int i = 1; i <= 50; i++)
		{
			System.out.println("Card #" + i + "\n********");
			System.out.println();
		
			for (int j = 1; j <= 24; j++ )
			{
					int bingoNum;
					bingoNum = bingo.nextInt(75);
					System.out.print("   " + bingoNum);
			}
			System.out.println();
			System.out.println();
		}
	}

}

Basically, I wanna place the 24 numbers on 5 rows, 5 columns, with the 3rd row/3rd column blank, just like a bingo card setup is.

How do I tell it to do a new line every 5th RANDOM number, plus a space in between the 2nd number and 4th number on the 3rd row, to look exactly like a bingo card?

As you see below, the output for the first 3 cards (out of 50) place all 24 random numbers (1-75) on the same line. Help??

Card #1
********

44 32 11 31 19 21 60 58 14 54 2 25 64 58 5 2 5 48 51 0 23 5 46 32

Card #2
********

19 50 7 54 39 4 55 72 6 40 26 65 8 11 10 6 58 62 34 65 48 5 72 28

Card #3
********

62 39 43 12 45 30 74 73 51 34 26 24 39 28 45 63 64 21 41 2 30 17 45 28

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
1
 

So do you want the placement of the new line to be randomized? Or do you want it to look a certain way? You've just suggested two different things...

Member Avatar
musikluver4
Junior Poster in Training
82 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I want the placement of the new line to look a certain way, every 5th number , start a new line.....like this

23 75 67 45 3
66 32 26 12 29
74 55 "sp" 13 11
1 20 19 33 44

"sp" meaning a space or blank, if all i can get is a new line every 5 numbers, that'll be great

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

Ok, then consider this example:

for (int i = 0; i < 25; i++){
if (i % 5 == 0) System.out.println();
}

If that doesn't make sense, look up the "modulus" operator on google. (Which is the operator I used above that looks like a percent sign).

Member Avatar
musikluver4
Junior Poster in Training
82 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

that's what I thought I had to use too, but didn't know to use 25, BUT, I've tried every which way I can on where to put it into the code, but I don't know where to put it......help?

Member Avatar
musikluver4
Junior Poster in Training
82 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

nevermind I got it.......now do i consider the same code you suggested for the space I would like to use 3rd row/3rd column?

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

Since you only need one space in the center of the card, it would be pointless to use "%" to do that. Just figure out which iteration of the for loop you need to put the space at. Then say

if (row == 3 && column == 3) System.out.print(" ");

That's just an example, I don't know the exact row and column to put it at, but that might be it.

Member Avatar
musikluver4
Junior Poster in Training
82 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm so sorry, I seem to always be asking for your help, but it seems this website is very good, meaning you, at helping others. For you information, I'm not even in a class, this is all just for fun, taking the two java class was over a year ago.

//Date: 3/17/2010

import java.util.Random;

public class Bingo 
{
	public static void main(String[] args) 
	{
		Random bingo = new Random();
		
		for (int i = 1; i <= 50; i++)
		{
			System.out.println("Card #" + i + "\n********");
			System.out.println();
			for (int j = 1; j <= 25; j++ )
			{	
				int bingoNum;
				bingoNum = bingo.nextInt(75);
				System.out.print("   " + bingoNum);
				if (j % 5 == 0)
				{
					System.out.println();
				}
			}
			System.out.println();
			System.out.println();
		}
	}

}

Putting the if statement you told me with the modulus

if (j % 5 == 0)
{
	System.out.println();
}

Did the right thing, but now each new row seems like start back over the Random function, because a lot of them have the same number in each 'card'
Example:
62 7 38 60 10
55 62 59 69 19
65 46 60 55 28
14 55 33 61 7
8 60 12 19 11
and also weirdly has the same number in the SAME ROW
and for some weird reason, some have '0' zero even though I have j = 1 in the for loop

for (int j = 1; j <= 25; j++ )

68 55 42 9 57
33 42 0 46 32
11 50 58 7 45
4 43 56 57 59
9 32 0 25 62

And one more thing, is it me or is most of the random numbers come in the 30 - 60 range, especlally 50-59? It's not very diverse. I know you can't see that, but I see it here.
Anything you can give me would be great!! If I can get this simple program down, who know what I can create!!!

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

Being 'random' doesn't necessarily mean it will be diverse over one try. But if you were to run it millions of times, you would see that the numbers would show up much more evenly. The for loop you have just defines how many times something happens. So

for (int i = 0; i < 10; i++){
//this happens 10 times!
}

for (int i = 1; i < 11; i++){
//this also happens 10 times!
}

However, you defined your Random as "bingo.nextInt(75)" which tells the random number generator to randomly generate a number between 0 and 74. Check out the link I just gave you - if you go to the nextInt(int) method, it says what I just told you. If you want your number to be between 1 and 75 you'd have to say

int result = bingo.nextInt(75) + 1;
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: