Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Java Help


  • Please log in to reply

#1
Tornel

Tornel

    Member

  • Member
  • PipPip
  • 27 posts
I'm trying to write a program in Java that can take specific high and low limits to a range of numbers, and still include those limits in the random number. I had an idea of how to do it, but I'm doing something wrong with it. Here is my code, maybe you guys can figure out what I'm doing wrong? Thanks

import java.util.Random;

public class RandomNumbers
{
public static void main(String[] args)
{
Random rand= new Random();

double low =Keyboard.readDouble("Enter the low limit for your random number(double)");
double high = Keyboard.readDouble("Enter the high limit for your random number(double)");

int low2 = Keyboard.readInt("Enter the high limit for your random number(int)");
int high2 = Keyboard.readInt("Enter the high limit for your random number(int)");

double x= randomDouble(rand, low, high);
int y= randomInt(rand, low2, high2);


System.out.println("Here is your random number(double): " + x);
System.out.println("Here is your random number(int): " + y);
}

public static double randomDouble(Random rand, int low, int high)
{
Random rand= new Random();
return ((high-low) * rand.nextDouble + low);

}

public static int randomInt(Random rand, int low2, int high2)
{
return rand.nextInt(high2 - low2 + 1) + low2;
}
}
  • 0

Advertisements


#2
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts

public static int randomInt(Random rand, int low2, int high2)
{
return rand.nextInt(high2 - low2 + 1) + low2;
}

You don't need to accept an instance of the Random class here, just create your own:
public int randomInt(int min, int max) {
	return new Random().nextInt(max - min + 1) + min;
}

public static double randomDouble(Random rand, int low, int high)
{
Random rand= new Random();
return ((high-low) * rand.nextDouble + low);

}

You're doing a lot of weird stuff here. First of all, your trying to create an instance of the Random class when you already are accepting one as a parameter. Then, you forgot parentheses after rand.nextDouble. There are a lot of ways to generate a random double, here's a simple approach (which I think you were trying to do):
public double randomDouble(int min, int max) {
	return ((max - min + 1) * new Random().nextDouble()) + min;
}
You could also use your randomInt(int, int) method:
public double randomDouble(int min, int max) {
	return randomInt(min, max) + new Random().nextDouble();
}

  • 0

#3
Tornel

Tornel

    Member

  • Topic Starter
  • Member
  • PipPip
  • 27 posts
Thanks! I got it now
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP