Solved

Java - how to generate Random numbers in while loop

Posted on 2012-03-10
6
463 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 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…
Suggested Courses

617 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