posted 8 years ago
An object of class Random (from package java.util) can do just that.
Random method nextInt comes in two forms:
nextInt(), with no arguments, returns an integer in the range 2,147,483,648 to + 2,147,483,647.
nextInt( scale ) returns an integer from 0 to scale  1. scale is the number of integers in the range.
To get a number from within a range that does not begin with 0 you must use a shifting value:
shift + nextInt( scale )
To get the scale for a range you need the total number of integers in that range. For example, the range
5 to 10 includes 6 integers ( 10  5 + 1 ).
Random method nextInt comes in two forms:
nextInt(), with no arguments, returns an integer in the range 2,147,483,648 to + 2,147,483,647.
nextInt( scale ) returns an integer from 0 to scale  1. scale is the number of integers in the range.
To get a number from within a range that does not begin with 0 you must use a shifting value:
shift + nextInt( scale )
To get the scale for a range you need the total number of integers in that range. For example, the range
5 to 10 includes 6 integers ( 10  5 + 1 ).
sahar sa
Ranch Hand
Posts: 102
Greg Stevens
Ranch Hand
Posts: 41
posted 8 years ago
nextDouble() returns a number between 0 (inclusive) and 1 (exclusive). This multiplied by ( end  start ) will give a number
between 0 and almost ( end  start ). Add that to start and the result is between start (inclusive) and end (exclusive).
By the way, I don't really know this for sure. I just looked it up in the API. I tried it in a for loop though, and it never gave me a number greater than or equal to end, nor did it give me a number less than start.
nextDouble() returns a number between 0 (inclusive) and 1 (exclusive). This multiplied by ( end  start ) will give a number
between 0 and almost ( end  start ). Add that to start and the result is between start (inclusive) and end (exclusive).
By the way, I don't really know this for sure. I just looked it up in the API. I tried it in a for loop though, and it never gave me a number greater than or equal to end, nor did it give me a number less than start.
sahar sa
Ranch Hand
Posts: 102
posted 8 years ago
hi,
Dear ritchie: sorry, let me explain again: I just need to generate a set of Double numbers between a range of [low,high] e.g. [3050]. I know that its too simple but I can not run it .
Dear steven :
1)I've tried to use this code: randomNumbers.nextDouble() as you said, but it gives me: nonstatic method cannot be referenced from a static context error. so I checked in internet an find an example said I have to make a constructor then instantiate it from main so I remove the nonstatic method eith referenced from static content. (If I've understand well!?) so I didi it as follow:
2) still got error: S() needs returned type!!! so I defined void then
3) still got 2 error: (1) class s (instantiate in main method ) can not be find (2) can not find class randomNumbers() .
I really dont know how to fix? that would be your kind if you show me an executable example of this!
sorry for long boring post.
thank you sooo much!
Sahar.
Dear ritchie: sorry, let me explain again: I just need to generate a set of Double numbers between a range of [low,high] e.g. [3050]. I know that its too simple but I can not run it .
Dear steven :
1)I've tried to use this code: randomNumbers.nextDouble() as you said, but it gives me: nonstatic method cannot be referenced from a static context error. so I checked in internet an find an example said I have to make a constructor then instantiate it from main so I remove the nonstatic method eith referenced from static content. (If I've understand well!?) so I didi it as follow:
2) still got error: S() needs returned type!!! so I defined void then
3) still got 2 error: (1) class s (instantiate in main method ) can not be find (2) can not find class randomNumbers() .
I really dont know how to fix? that would be your kind if you show me an executable example of this!
sorry for long boring post.
thank you sooo much!
Sahar.
posted 8 years ago
It seems that you're really confusing things.
Why do you make an instance of Random and call it 'max'? Why do you declare two variables 'low' and 'high' and then you don't use them for anything? Please make sure that you understand exactly what your own code does.
This is a simple problem, and the answer has already been given.
The method nextDouble() of class Random gives you random numbers between 0 and 1. If you want the number to be between 30 and 50 instead of 0 and 1, you simply multiply the result by the length of the interval (50  30 = 20) and add the start of the interval to it (30):
You could make a method out of it (this version uses Math.random(), which also returns a random double value between 0 and 1):
Why do you make an instance of Random and call it 'max'? Why do you declare two variables 'low' and 'high' and then you don't use them for anything? Please make sure that you understand exactly what your own code does.
This is a simple problem, and the answer has already been given.
The method nextDouble() of class Random gives you random numbers between 0 and 1. If you want the number to be between 30 and 50 instead of 0 and 1, you simply multiply the result by the length of the interval (50  30 = 20) and add the start of the interval to it (30):
You could make a method out of it (this version uses Math.random(), which also returns a random double value between 0 and 1):
Campbell Ritchie
Marshal
Posts: 57446
175
You don't like waffles? Well, do you like this tiny ad?
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
