Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java - how to generate Random numbers in while loop

Posted on 2012-03-10
6
Medium Priority
?
478 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

664 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