Solved

Java - how to generate Random numbers in while loop

Posted on 2012-03-10
6
406 Views
Last Modified: 2012-03-10
I want to generate two random numbers, then check for a condition, and as long as condition holds true, generate two new different random numbers.

This is what I have... and I suspect it's probably wrong...

Random hit = new Random();
				int rowRan = hit.nextInt(9);
				int colRan = hit.nextInt(9);
				
				while(ships[rowRan][colRan] == '-'  || ships[rowRan][colRan] == '*'){
					Random nexthit = new Random();
					rowRan = nexthit.nextInt(9);
					colRan = nexthit.nextInt(9);
				}
				rowInt = rowRan;
				colInt = colRan;

Open in new window

0
Comment
Question by:Kyle Hamilton
  • 4
  • 2
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37706076
you can do that, i believe, but you actually don't need to craete new Rndom every time:
Random hit = new Random();
				int rowRan = hit.nextInt(9);
				int colRan = hit.nextInt(9);
				
				while(ships[rowRan][colRan] == '-'  || ships[rowRan][colRan] == '*'){
					
					rowRan = hit.nextInt(9);
					colRan = hit.nextInt(9);
				}
				rowInt = rowRan;
				colInt = colRan;
                                  

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37706080
Random hit = new Random();

creates new random value using the cvurren millisecond as a seed,
after that each hit.nextInt(9) will generate a random int less than 9
you do not need to recreate the Random object every time - it will generate different random int less than 9 eech time ytou call it, so you don;t need ti recreate it every time
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37706084
As it will be useing the current time as a seed every time and current time will be changing so, igf you would be creating the new Random every time it will have diofferent seed and will generate bew sequence, so probably it should not maytter in thuis case, but anyway you don't need to create new Random object every time
You create it once and then generate the random integers as much as you want
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 37706085
I was hoping for that.

So when is a Random generated with the same "seed" giving you the same random number?

(not sure what I'm talking about... it's something I read..)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37706088
yes,
if you say

Random r = new Random(25345123);

and then you say

int i = r.next(100);
let's assume each time you'll be getting as i say

41, 13, 12, 56...


then somehwre down the roda you again create the Random with the same seed


r = new Random(25345123);

and again do

i = r.nextInt(100);

several tiumes, then you'are guranteed that you'll be getting the same sequence:

41, 13, 12, 56...

That is waht mesans seed. If you use different seemd then sequence will be different

But within each sequence numbers are still coming in random way
So you don't need to create Random object again
0
 
LVL 25

Author Closing Comment

by:Kyle Hamilton
ID: 37706089
cool. That's exactly what I wanted to know....

Thanks.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
factorial example challenge 10 61
countX 22 70
json example 39 113
Free Alternative to JIRA 4 55
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now