免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1959 | 回复: 5
打印 上一主题 下一主题

Random类能创建出混合数据类型吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-01-12 16:02 |只看该作者 |倒序浏览
看了一下random类的API  发现都是创建单一数据类型的 要么nextInt得到int类型,要么nextFloat得到float类型  如果我想得到一组数据 其中又有float 又有int之类的  有没有什么方法呢?  譬如:随机产生1000个100以内的数  挑出其中的整数这类的问题   谢谢~

论坛徽章:
0
2 [报告]
发表于 2006-01-12 17:46 |只看该作者
随机调用这些方法不就可以了么?

论坛徽章:
0
3 [报告]
发表于 2006-01-12 17:55 |只看该作者
数字类型不应该成为壁垒啊
你可以取整啊,然后和原数相减,减下来等于零的就是整数了啊。当然,浮点数的精度误差还是会有些影响的,但是影响到什么程度,看你的需要了。你取的精度越高,误差越小。

论坛徽章:
0
4 [报告]
发表于 2006-01-12 17:58 |只看该作者

  1. package test.util;

  2. import java.util.Calendar;
  3. import java.util.Random;

  4. public class MixedTypeRandom extends Random {
  5.         public static final int TYPE_DOUBLE = 0;

  6.         public static final int TYPE_FLOAT = 1;

  7.         public static final int TYPE_INTEGER = 2;

  8.         public static final int TYPE_LONG = 3;

  9.         private static final int TYPE_COUNT = 4;

  10.         Random r = null;

  11.         public MixedTypeRandom(long timeInMillis) {
  12.                 super(timeInMillis);
  13.                 r = new Random(timeInMillis);
  14.         }

  15.         public static void main(String[] args) {
  16.                 MixedTypeRandom mtr = new MixedTypeRandom(Calendar.getInstance()
  17.                                 .getTimeInMillis());
  18.                 for (int i = 0; i < 10; i++) {
  19.                         Number n = mtr.nextNumber();
  20.                         System.out.println("Random Number[" + i + "]: " + n);
  21.                 }
  22.         }

  23.         public Number nextNumber() {
  24.                 Number n = null;
  25.                 int type = r.nextInt(MixedTypeRandom.TYPE_COUNT);

  26.                 System.out.println("type:" + r);
  27.                 switch (type) {
  28.                 case MixedTypeRandom.TYPE_DOUBLE:
  29.                         n = new Double(this.nextDouble());
  30.                         break;
  31.                 case MixedTypeRandom.TYPE_FLOAT:
  32.                         n = new Float(nextFloat());
  33.                         break;
  34.                 case MixedTypeRandom.TYPE_INTEGER:
  35.                         n = new Integer(nextInt());
  36.                         break;
  37.                 case MixedTypeRandom.TYPE_LONG:
  38.                         n = new Long(nextLong());
  39.                         break;
  40.                 }
  41.                 return n;
  42.         }
  43. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2006-01-13 08:59 |只看该作者
4 楼的斑竹 真强啊!呵呵!学习中

论坛徽章:
0
6 [报告]
发表于 2006-01-13 19:44 |只看该作者
原帖由 sakulagi 于 2006-1-12 17:58 发表
[code]
package test.util;

import java.util.Calendar;
import java.util.Random;

public class MixedTypeRandom extends Random {
        public static final int TYPE_DOUBLE = 0;

        public static fina ...


谢谢老大 完全明白了

思路真宽吖。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP