Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java - how to generate Random numbers in while loop

Posted on 2012-03-10
6
Medium Priority
?
502 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
This video teaches viewers about errors in exception handling.
Suggested Courses

824 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