产生随机数的Random类的框架

package com.random.cc;

/**
 * 特别说明随机数包含两个严格的性质
 * 两个连续的随机数之和为偶数或奇数的概率相等
 * 如果随机产生1000个数,这些数中是有重复的(大约368个数从不出现)
 */

// CONSTRUCTION: with (a) no initializer or (b) an integer
//     that specifies the initial state of the generator
//
// ******************PUBLIC OPERATIONS*********************
//     Return a random number according to some distribution:
// int randomInt( )                     --> Uniform, 1 to 2^31-1
// int random0_1( )                     --> Uniform, 0 to 1
// int randomInt( int low, int high )   --> Uniform low..high
// long randomLong( long low, long high ) --> Uniform low..high
// void permute( Object [ ] a )         --> Randomly permutate

/**
 * Random number class, using a 31-bit linear congruential generator. Note that
 * java.util contains a class Random, so watch out for name conflicts.
 * 
 * @author SunnyBoy
 * @version Time:2017年7月25日 上午11:12:06
 */
public class Random {
    /**
     * 下面的常量,除非特别说说明,最好不要修改它们的数值,此取值是经过深入研究的
     */
    private static final int A = 48271;
    private static final int M = 2147483647;
    private static final int Q = M / A;//44488
    private static final int R = M % A;//3399

    public static void main(String[] args) {
        Random r = new Random(1);

        for (int i = 0; i < 20; i++)
            System.out.println(r.randomInt());
    }

    /**
     * Construct this Random object with initial state obtained from system clock.
     */
    public Random() {
        this((int) (System.currentTimeMillis() % Integer.MAX_VALUE));
    }

    /**
     * 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 randomInt() {
        int tmpState = A * (state % Q) - R * (state / Q);
        if (tmpState >= 0)
            state = tmpState;
        else
            state = tmpState + M;

        return state;
    }

    /**
     * Return a pseudorandom int, and change the internal state. DOES NOT WORK.
     * 
     * @return the pseudorandom int.
     */
    public int randomIntWRONG() {
        return state = (A * state) % M;
    }

    /**
     * Return a pseudorandom double in the open range 0..1 and change the internal
     * state.
     * 
     * @return the pseudorandom double.
     */
    public double random0_1() {
        return (double) randomInt() / 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 randomInt(int low, int high) {
        double partitionSize = (double) M / (high - low + 1);

        return (int) (randomInt() / partitionSize) + low;
    }

    /**
     * Return an 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 randomLong(long low, long high) {
        long longVal = ((long) randomInt() << 31) + randomInt();
        long longM = ((long) M << 31) + M;

        double partitionSize = (double) longM / (high - low + 1);
        return (long) (longVal / partitionSize) + low;
    }

    /**
     * Randomly rearrange an array. The random numbers used depend on the time and
     * day.
     * 
     * @param a
     *            the array.
     */
    public static void permute(Object[] a) {
        Random r = new Random();

        for (int j = 1; j < a.length; j++)
            swapReferences(a, j, r.randomInt(0, j));
    }

    private static void swapReferences(Object[] a, int i, int j) {
        Object tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }

    private int state;

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值