4
public class TestSample {
    public static void main(String[] args) { 

        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);

        double ran = Math.random();



    }
}

I don't want to use Random r = new Random(); class. Is there any other way to generate random numbers. I am just struck with what logic could be applied to generate random numbers between two numbers.

Ashkan Aryan
  • 3,504
  • 4
  • 30
  • 44
John Cooper
  • 7,343
  • 31
  • 80
  • 100
  • 2
    http://stackoverflow.com/questions/363681/java-generating-random-number-in-a-range – Alex K Sep 15 '11 at 07:55
  • Can I ask why you don't want to use Random class? – Ashkan Aryan Sep 15 '11 at 08:04
  • 1
    Don't want to use the `Random` class? Consider using this instead: http://xkcd.com/221/ – NPE Sep 15 '11 at 08:08
  • http://stackoverflow.com/questions/2872138/is-there-good-prng-generating-values-without-hidden-state
    http://stackoverflow.com/questions/190135/open-source-random-number-generation-algorithm-in-c
    – Jan S Sep 15 '11 at 08:10

4 Answers4

8

It's really easy... you only need to figure out which is the minimum value and what is the difference between the two numbers (let's call it diff). Then, you can scale the Math.random value (between 0 and 1) by multiplying by diff (now its range is between 0 and diff). Then, if you add the minimum value, your range is between min and min + diff (which is the other value)

int min = min(a,b);
int max = max(a,b);

int diff = max - min;

int result = min + diff * Math.random();
Matteo
  • 1,367
  • 7
  • 25
5

Consider using this code:

int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
double ran = Math.random();
double random;

if(a < b)
    random = (b-a)*ran + a;
else
    random = (a-b)*ran + b;

This will work for a >= 0 and b >= 0 if you consider using negative number the logic sligtly changes

Stefano
  • 3,981
  • 8
  • 36
  • 66
  • 1
    +1: You don't need to check which is greater or whether they are positive. If `a` and `b` are say '1' and '5' you will get a random number between '1' and '5 or between '5' and '1' The result should be effectively the same. – Peter Lawrey Sep 15 '11 at 08:49
  • yes, you right... because then the difference becom negative and get subtracred to the big one. feel lazy of testing the behaviour with negative numbers...:p – Stefano Sep 15 '11 at 09:09
0

If you are expecting a double result, the simplest approach is

int a =
int b =
double result = (a-b)*Math.random() + b;

It doesn't matter which is greater as you get the same distribution.

However, if you want a random integer between 'a' and 'b' is a bit more complex.

int a = 
int b =
int result = Math.floor((Math.abs(a-b)+1) * Math.random()) + Math.min(a, b);

The reason the result is different is that a random double between 0 and 1 will be just less than 1 i.e. [0.0, 1.0) However a random integer between 1 and 6 usually includes 1, 2, 3, 4, 5, 6 equally. As a decimal this is the round down of [0.0 ... 7.0)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

You may get white noise from your microphone, and take any number from there. After that you may take any number from the given data, and do with it what you want. The example of getting data from the microphone can be found here.

wanderlust
  • 1,826
  • 1
  • 21
  • 25