Home arrow static arrow Java Programming [Archive] - generate a random number between 0 and 2
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - generate a random number between 0 and 2
This topic has 4 replies on 1 page.

Posts:2
Registered: 7/14/04
generate a random number between 0 and 2  
Jul 14, 2004 8:14 AM



 
I need help on an array without a for loop to generate a number between 0 and 2. The number represents winner responses in a game.
 

Posts:31,095
Registered: 4/30/99
Re: generate a random number between 0 and 2  
Jul 14, 2004 8:17 AM (reply 1 of 4)



 
int[] responses = new int[3] {1, 1, 1};
(What is your objection to for-loops? Can we use a while-loop to fill the array?)
 

Posts:22
Registered: 3/2/00
Re: generate a random number between 0 and 2  
Jul 14, 2004 8:54 AM (reply 2 of 4)



 
Do you have to use an array? If you just want a random number between 0 and 2 something like this will do it:

int random = (int)(Math.random() * 2)
 

Posts:3,258
Registered: 00-08-28
Re: generate a random number between 0 and 2  
Jul 14, 2004 9:02 AM (reply 3 of 4)



 
Do you have to use an array? If you just want a random
number between 0 and 2 something like this will do it:

int random = (int)(Math.random() * 2)

I would rather advise using class Random and against using the Math.random. Heres a method in the APi that you could use

public int nextInt(int n)Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned. All n possible int values are produced with (approximately) equal probability. The method nextInt(int n) is implemented by class Random as follows:  public int nextInt(int n) {     if (n<=0)		throw new IllegalArgumentException("n must be positive");      if ((n & -n) == n)  // i.e., n is a power of 2         return (int)((n * (long)next(31)) >> 31);      int bits, val;     do {         bits = next(31);         val = bits % n;     } while(bits - val + (n-1) < 0);     return val; } The hedge "approximately" is used in the foregoing description only because the next method is only approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen bits, then the algorithm shown would choose int values from the stated range with perfect uniformity.  The algorithm is slightly tricky. It rejects values that would result in an uneven distribution (due to the fact that 2^31 is not divisible by n). The probability of a value being rejected depends on n. The worst case is n=2^30+1, for which the probability of a reject is 1/2, and the expected number of iterations before the loop terminates is 2.  The algorithm treats the case where n is a power of two specially: it returns the correct number of high-order bits from the underlying pseudo-random number generator. In the absence of special treatment, the correct number of low-order bits would be returned. Linear congruential pseudo-random number generators such as the one implemented by this class are known to have short periods in the sequence of values of their low-order bits. Thus, this special case greatly increases the length of the sequence of values returned by successive calls to this method if n is a small power of two. Parameters:n - the bound on the random number to be returned. Must be positive.Returns:a pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive).Throws:IllegalArgumentException - n is not positive.
 

Posts:24,036
Registered: 2/3/03
Re: generate a random number between 0 and 2  
Jul 14, 2004 9:04 AM (reply 4 of 4)



 
So much for the 'preview' button. ;o)
 
This topic has 4 replies on 1 page.