Random Integer Generation

Random Integer Generation
Question :
How do I write a Java program that generates random numbers within limits, such as 1 to 100, or 1 to 1000?

Answer :
The java.util package includes a Random class which will generate a sequence of pseudo-random numbers. Pseudo-random number sequences appear to have a random distribution, but have a definite order. Given the same seed, a pseudo-random number generator will always produce the same sequence of numbers. Therefore, you should initialize the Random class with as random a seed as possible. Using the current time as a seed is often sufficient.

However, when a pseudo-random number sequence is exhausted, it wraps around and starts again from the beginning. Consequently, for long-lived applications that generate many random numbers over time, you will want to periodically reseed your random generator.

The Random class does not provide a generic means of generating random integers within a specific range. Rather, it generates a uniformly distributed set of values between 0 and some upper bound. For doubles and floats it generates a value between 0 and 1. You can use this to generate arbitrary ranges of random integers. The following example program provides a random integer generating class that takes care of converting a random double into a random integer within a specific range. It also takes care of reseeding the random number generator after the sequence has been in use for a long time.

import java.util.*;

public final class RandomIntGenerator {
  public static final int DEFAULT_MIN_RANGE = 1;
  public static final int DEFAULT_MAX_RANGE = 100;

  int _minRange, _maxRange, _range;
  long _numCalls;
  Random _random;

  public RandomIntGenerator() {
    this(DEFAULT_MIN_RANGE, DEFAULT_MAX_RANGE);
  }

  public RandomIntGenerator(int minRange, int maxRange) {
    _random   = new Random(System.currentTimeMillis());
    setRange(minRange, maxRange);
    _numCalls = 0;
  }

  public void setRange(int minRange, int maxRange) {
    _minRange = minRange;
    _maxRange = maxRange;
    _range    = maxRange - minRange + 1;
  }

  public int nextInt() {
    double d;
    d = ((double)_range)*_random.nextDouble();
    
    if(++_numCalls == Integer.MAX_VALUE) {
      // The pseudo-random number sequence is sufficiently
      // exhausted that it is time to switch to a new seed.
      _numCalls = 0;
      _random.setSeed(System.currentTimeMillis());
    }

    return (_minRange + (int)d);
  }

  public static final void main(String[] args) {
    RandomIntGenerator randomInt;

    randomInt = new RandomIntGenerator();

    for(int i = RandomIntGenerator.DEFAULT_MIN_RANGE;
        i <= RandomIntGenerator.DEFAULT_MAX_RANGE; ++i)
      System.out.println(randomInt.nextInt());
  }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值