4 Replies Latest reply: Dec 20, 2006 7:03 AM by 807607 RSS

    generate random numbers without java.util.random

    807607
      I always use java.util.random but now, I have to make a function that generate a random number from another number.

      The problem is that I�ve to do it in C and in Java.

      Can anyone give me a a function that do this?

      thanks.
        • 1. Re: generate random numbers without java.util.random
          807607
          I use number 157 in page

          http://www.iro.umontreal.ca/~lecuyer/papers.html

          It is easy to implement in C, C++ and Java.
          • 2. Re: generate random numbers without java.util.random
            807607
            Do you want random integers within a certain range, or floats, either between 0-1 or within a certain range?

            Here's a class that can do most of it, which could of course easily be condenced to a static class that does just what you want it to do

            Hope it helps.
            public class Random {
                private static final int A = 48271;
                private static final int M = 2147483647;
                private static final int Q = M / A;
                private static final int R = M % A;
            
                private int state;
            
                /**
                 * Construct this Random object with
                 * specified initial state.
                 * @param initialValue the initial state.
                 */
                public Random( int initialValue ) {
                    if( initialValue < 0 )
                        initialValue += M;
            
                    state = initialValue;
                    if( state <= 0 )
                        state = 1;
                }
                /**
                 * Return a pseudorandom int, and change the
                 * internal state.
                 * @return the pseudorandom int.
                 */
                public int nextInt( ) {
                    int tmpState = A * ( state % Q ) - R * ( state / Q );
                    if( tmpState >= 0 )
                        state = tmpState;
                    else
                        state = tmpState + M;
            
                    return state;
                }
                
                /**
                 * Return a pseudorandom int in range [0..high),
                 * and change the internal state.
                 * @return the pseudorandom int.
                 */
                public int nextInt( int high ) {
                    return (int) ( nextLong( ) % high );
                }
                /**
                 * Return a pseudorandom double in the open range 0..1
                 * and change the internal state.
                 * @return the pseudorandom double.
                 */
                public double nextDouble( ) {
                    return (double) nextInt( ) / M;
                }
            
                /**
                 * Return an int in the closed range [low,high], and
                 * change the internal state.
                 * @param low the minimum value returned.
                 * @param high the maximum value returned.
                 * @return the pseudorandom int.
                 */
                public int nextInt( int low, int high ) {
                    double partitionSize = (double) M / ( high - low + 1 );
            
                    return (int) ( nextInt( ) / partitionSize ) + low;
                }
            
                /**
                 * Return a long in the range [0, 2^62-1], and
                 * change the internal state.
                 * @return the pseudorandom long.
                 */
                public long nextLong( ) {
                    return  ( (long) nextInt( ) << 31 ) + nextInt( );
                }
            
                /**
                 * Return a long in the closed range [low,high], and
                 * change the internal state.
                 * @param low the minimum value returned.
                 * @param high the maximum value returned.
                 * @return the pseudorandom long.
                 */
                public long nextLong( long low, long high ) {
                    long longVal =  ( (long) nextInt( ) << 31 ) + nextInt( );
                    long longM =  ( (long) M << 31 ) + M;
            
                    double partitionSize = (double) longM / ( high - low + 1 );
                    return (long) ( longVal / partitionSize ) + low;
                }
            }
            • 3. Re: generate random numbers without java.util.random
              807607
              Slow response => Double post...
              • 4. Re: generate random numbers without java.util.random
                807607
                thanks to both. I�m now testing what you say me.