随机数发生器 : 随机 « 开发相关类 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 开发相关类 » 随机屏幕截图 
随机数发生器
 
/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

/**
 * This class defines methods for computing pseudo-random numbers, and defines
 * the state variable that needs to be maintained for use by those methods.
 */
public class Randomizer {
  // Carefully chosen constants from the book "Numerical Recipes in C".
  // All "static final" fields are constants.
  static final int m = 233280;

  static final int a = 9301;

  static final int c = 49297;

  // The state variable maintained by each Randomizer instance
  int seed = 1;

  /**
   * The constructor for the Randomizer() class. It must be passed some
   * arbitrary initial value or "seed" for its pseudo-randomness.
   */
  public Randomizer(int seed) {
    this.seed = seed;
  }

  /**
   * This method computes a pseudo-random number between 0 and 1 using a very
   * simple algorithm. Math.random() and java.util.Random are actually a lot
   * better at computing randomness.
   */
  public float randomFloat() {
    seed = (seed * a + c% m;
    return (floatMath.abs((floatseed / (floatm);
  }

  /**
   * This method computes a pseudo-random integer between 0 and specified
   * maximum. It uses randomFloat() above.
   */
  public int randomInt(int max) {
    return Math.round(max * randomFloat());
  }

  /**
   * This nested class is a simple test program: it prints 10 random ints.
   * Note how the Randomizer object is seeded using the current time.
   */
  public static void main(String[] args) {
      Randomizer r = new Randomizer((intnew java.util.Date().getTime());
      for (int i = 0; i < 10; i++)
        System.out.println(r.randomInt(100));

  }
}
           
         
  
Related examples in the same category
1. 创建随机数
2. 创建两个随机数生成相同的种子
3. Math.random ( )生产0.0和1.0
4. 随机数1到100之间
5. 随机数介于0和10
6. 随机整数,范围为从0到n
7. Random.nextInt(n) returns a distributed int value between 0 (inclusive) and n (exclusive).
8. 使用Math.round四舍五入浮动Java和双为精切树脂码
9. nextDouble() and nextGaussian() in java.util.Random
10. 生成随机数
11. 产生随机整数类 Random()
12. 使用随机数: nextGaussian
13. 产生一个随机数字数组
14. 下一个双为精确和整数类数值
15. 随机字节
16. 随机布尔型变量
17. 随机长整型数值
18. 随机浮点型数值
19. Random double type number
20. Math.random
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.