首页

关于instinct源码包定义RandomProvider随机数实现类提供区间范围、任意基础数据类型随机数生成源码实现

标签:instinct,随机数,RandomProviderImpl,googlecode     发布时间:2018-07-08   

一、前言

关于instinct源码包com.googlecode.instinct.internal.actor.RandomProvider、com.googlecode.instinct.internal.actor.RandomProviderImpl随机数接口定义及实现类,可以生成对指定任意数据类型随机数,对指定最小最大值范围区间生成随机数,参见下面源码说明部分。

二、源码说明

1.RandomProvider接口

package com.googlecode.instinct.internal.actor;@b@@b@public abstract interface RandomProvider@b@{@b@  public abstract <T> T randomValue(Class<T> paramClass);@b@@b@  public abstract int randomIntInRange(int paramInt1, int paramInt2);@b@}

2.RandomProviderImpl实现类

package com.googlecode.instinct.internal.actor;@b@@b@import java.util.Random;@b@@b@public final class RandomProviderImpl@b@  implements RandomProvider@b@{@b@  private Random random;@b@@b@  public RandomProviderImpl()@b@  {@b@    this.random = new Random();@b@  }@b@@b@  public <T> T randomValue(Class<T> type)@b@  {@b@    if (type == Boolean.class)@b@      return randomBoolean();@b@@b@    if (type == Integer.class)@b@      return randomInteger();@b@@b@    if (type == Long.class)@b@      return randomLong();@b@@b@    if (type == Float.class)@b@      return randomFloat();@b@@b@    if (type == Double.class)@b@      return randomDouble();@b@@b@    if (type == Byte.class)@b@      return randomByte();@b@@b@    if (type == String.class)@b@      return randomString();@b@@b@    if (type == Class.class)@b@      return randomClass();@b@@b@    if (type == Object.class)@b@      return randomObject();@b@@b@    throw new UnsupportedOperationException("Hmm.  I cannot provide an instance of '" + type + "'.  " + "Might be worth edgifying (hiding behind an interface) this type or talking to the boosters!");@b@  }@b@@b@  public int randomIntInRange(int min, int max)@b@  {@b@    if (min >= max) {@b@      throw new IllegalArgumentException("min of range must be less than max");@b@    }@b@@b@    int partitionSize = max - min + 1;@b@    int intInPartition = this.random.nextInt(partitionSize);@b@    return (intInPartition + min);@b@  }@b@@b@  private Object randomClass()@b@  {@b@    return InternalInterface.class;@b@  }@b@@b@  private Object randomString() {@b@    return "Some random string " + this.random.nextLong();@b@  }@b@@b@  private Object randomBoolean() {@b@    return Boolean.valueOf(this.random.nextBoolean());@b@  }@b@@b@  private Object randomInteger() {@b@    return Integer.valueOf(this.random.nextInt());@b@  }@b@@b@  private Object randomLong() {@b@    return Long.valueOf(this.random.nextLong());@b@  }@b@@b@  private Object randomFloat() {@b@    return Float.valueOf(this.random.nextFloat());@b@  }@b@@b@  private Object randomDouble() {@b@    return Double.valueOf(this.random.nextDouble());@b@  }@b@@b@  private Object randomByte() {@b@    byte[] bytes = new byte[1];@b@    this.random.nextBytes(bytes);@b@    return Byte.valueOf(bytes[0]);@b@  }@b@@b@  private Object randomObject() {@b@    return new Object();@b@  }@b@@b@  private static abstract interface InternalInterface@b@  {@b@  }@b@}
<<推荐下载>>
  • (1) 阿里巴巴Java开发手册8种不同版本
  • (2) Web前端开发视频教程
  • (3) 30+明星讲师PPT课件分享一线大厂架构实战经验
  • (4) java开发_架构篇_视频资源分享_v2208
  • (5) java开发_高级篇_视频资源分享_v2208
  • (6) java开发_进阶篇(中级)_视频资源分享_v2208
  • (7) java开发_入门篇_视频资源分享_v2208
  • (8) 微信小程序开发视频1+167源码+实战demo等下载
  • (9) easy-shopping电子商务java源码(附脚本和安装文档说明)下载
  • (10) java常用的72份知名实用的电子书下载
  • (11) java开发性能优化资料整理大全(8份电子文档+3份实战优化)下载
  • (12) 9个常用的算法设计资料和100以上视频课件内容下载
  • (13) vue开发必备常用手册16件下载
  • (14) 21种不同技术集群方案(es、flink、redis、nginx、zk、lvs、kafka、mysql、k8s等)参考资料下载
  • (15) 20种技术代码规范(js/java/dba/阿里/华为/oracle/mysql等)参考资料下载
  • (16) 微服务五套资料(0-1,架构设计,springcloud,nacos等)下载
  • (17) 架构师(28知识图谱+3套简历模板+6套架构实战文档等)完整资料整理下载
  • (18) 大数据18套实战基础知识+8套简历模板下载
  • (19) 并发编程全套(7套+阿里巴巴+亿级实战等)实战资料下载
  • (20) Kafka九套学习整理知识点全套(面试+笔记+代码api+命令+容备等)资料下载
  • (21) java全套9个不同方向类型的面试题(基础+核心+大厂+架构师+近万套题库等)下载
  • (22) JAVA开发常用API帮助文档大全(超52种以上技术资料,高手必备)下载
  • (23) springcloud超详细139件全套学习实战资料( 视频课件+源码demo+文档资料等)下载
  • 更多推荐>>
  • <<热门文章>>