Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
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