本文整理了Java中java.util.Random.<init>()
方法的一些代码示例,展示了Random.<init>()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Random.<init>()
方法的具体详情如下:
包路径:java.util.Random
类名称:Random
方法名:<init>
[英]Constructs a random generator with an initial state that is unlikely to be duplicated by a subsequent instantiation.
The initial state (that is, the seed) is partially based on the current time of day in milliseconds.
[中]构造一个初始状态不太可能被后续实例化复制的随机生成器。
初始状态(即种子)部分基于当前时间(以毫秒为单位)。
代码示例来源:origin: skylot/jadx
private static String generateTagAttrName() {
final int length = 6;
Random r = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= length; i++) {
sb.append((char) (r.nextInt(26) + 'a'));
}
return sb.toString();
}
代码示例来源:origin: apache/incubator-shardingsphere
@Override
public String getDataSource(final String name, final String masterDataSourceName, final List<String> slaveDataSourceNames) {
return slaveDataSourceNames.get(new Random().nextInt(slaveDataSourceNames.size()));
}
}
代码示例来源:origin: chrisbanes/cheesesquare
private List<String> getRandomSublist(String[] array, int amount) {
ArrayList<String> list = new ArrayList<>(amount);
Random random = new Random();
while (list.size() < amount) {
list.add(array[random.nextInt(array.length)]);
}
return list;
}
代码示例来源:origin: prestodb/presto
private List<String> createVarcharValuesWithNulls()
{
Random random = new Random();
List<String> values = new ArrayList<>();
for (int i = 0; i < ROWS; ++i) {
if (random.nextBoolean()) {
values.add(Strings.repeat("0", 4));
}
else {
values.add(null);
}
}
return values;
}
}
代码示例来源:origin: alibaba/arthas
public static void main(String[] args) throws InterruptedException {
List<Pojo> list = new ArrayList<Pojo>();
for (int i = 0; i < 40; i ++) {
Pojo pojo = new Pojo();
pojo.setName("name " + i);
pojo.setAge(i + 2);
list.add(pojo);
}
System.out.println(p);
while (true) {
int random = new Random().nextInt(40);
String name = list.get(random).getName();
list.get(random).setName(null);
test(list);
list.get(random).setName(name);
Thread.sleep(1000L);
}
}
代码示例来源:origin: ReactiveX/RxJava
@Test(timeout = 30000)
public void flatMapRangeMixedAsyncLoop() {
for (int i = 0; i < 2000; i++) {
if (i % 10 == 0) {
System.out.println("flatMapRangeAsyncLoop > " + i);
final Random rnd = new Random();
@Override
public Observable<Integer> apply(Integer t) {
System.out.println(to.valueCount());
if (list.size() < 1000) {
Set<Integer> set = new HashSet<Integer>(list);
for (int j = 0; j < 1000; j++) {
if (!set.contains(j)) {
System.out.println(j + " missing");
assertEquals(1000, list.size());
代码示例来源:origin: google/guava
public void testIteratorTester() throws Exception {
Random random = new Random(0);
List<Integer> list = Lists.newArrayList();
for (int i = 0; i < 3; i++) {
list.add(random.nextInt());
}
runIterator(list, 6);
}
代码示例来源:origin: stackoverflow.com
Random r = new Random();
Map<Boolean, List<String>> groups = stream
.collect(Collectors.partitioningBy(x -> r.nextBoolean()));
System.out.println(groups.get(false).size());
System.out.println(groups.get(true).size());
代码示例来源:origin: google/guava
public void testArbitrary_withCollisions() {
List<Integer> list = Lists.newArrayList();
for (int i = 0; i < 50; i++) {
list.add(i);
}
Ordering<Object> arbitrary =
new ArbitraryOrdering() {
@Override
int identityHashCode(Object object) {
return ((Integer) object) % 5; // fake tons of collisions!
}
};
// Don't let the elements be in such a predictable order
list = shuffledCopy(list, new Random(1));
Collections.sort(list, arbitrary);
// Now we don't care what order it's put the list in, only that
// comparing any pair of elements gives the answer we expect.
Helpers.testComparator(arbitrary, list);
}
代码示例来源:origin: stanfordnlp/CoreNLP
public static void main(String[] args) {
Properties config = StringUtils.argsToProperties(args);
log.info(config);
boolean fullSentence = PropertiesUtils.getBool(config, "fullSentence", false);
Random random = new Random();
String tagSeparator = config.getProperty("tagSeparator", TaggerConfig.TAG_SEPARATOR);
TaggedFileRecord record = TaggedFileRecord.createRecord(config, config.getProperty("input"));
for (List<TaggedWord> sentence : record.reader()) {
int len = random.nextInt(sentence.size()) + 1;
System.out.println(SentenceUtils.listToString(sentence.subList(0, len), false, tagSeparator));
if (fullSentence) {
System.out.println(SentenceUtils.listToString(sentence, false, tagSeparator));
}
}
}
代码示例来源:origin: stackoverflow.com
int[] array = new int[10];
Random rand = new Random();
for (int i = 0; i < array.length; i++)
array[i] = rand.nextInt(100) + 1;
Arrays.sort(array);
System.out.println(Arrays.toString(array));
// in reverse order
for (int i = array.length - 1; i >= 0; i--)
System.out.print(array[i] + " ");
System.out.println();
代码示例来源:origin: google/guava
public void testPercentiles_indexes_varargsAll_compute_doubleCollection() {
ArrayList<Integer> indexes = new ArrayList<>();
ImmutableMap.Builder<Integer, Double> expectedBuilder = ImmutableMap.builder();
for (int index = 0; index <= 100; index++) {
indexes.add(index);
expectedBuilder.put(index, expectedLargeDatasetPercentile(index));
}
Random random = new Random(770683168895677741L);
Collections.shuffle(indexes, random);
assertThat(percentiles().indexes(Ints.toArray(indexes)).compute(PSEUDORANDOM_DATASET))
.comparingValuesUsing(QUANTILE_CORRESPONDENCE)
.containsExactlyEntriesIn(expectedBuilder.build());
}
代码示例来源:origin: thinkaurelius/titan
@Test
public void testTimeSequence() throws Exception {
Random r = new Random();
Instant[] times = new Instant[10];
for (int i = 0; i < times.length; i++) {
times[i] = TimestampProviders.NANO.getTime();
if (i > 0) assertTrue(times[i] + " > " + times[i - 1], times[i].compareTo(times[i - 1])>0);
Thread.sleep(r.nextInt(50) + 2);
}
}
代码示例来源:origin: apache/incubator-druid
@Test
public void testOrderByWithLimit()
{
List<String> expected = rawInput.subList(0, Math.min(limit, rawInput.size()));
List<String> inputs = Lists.newArrayList(rawInput);
Collections.shuffle(inputs, new Random(2));
Sequence<String> result = new TopNSequence<String>(Sequences.simple(inputs), ordering, limit);
Assert.assertEquals(expected, result.toList());
}
}
代码示例来源:origin: org.apache.commons/commons-lang3
@Test
public void testLANG805() {
final long seed = System.currentTimeMillis();
assertEquals("aaa", RandomStringUtils.random(3,0,0,false,false,new char[]{'a'},new Random(seed)));
}
代码示例来源:origin: LitePalFramework/LitePal
private void saveAllByRandom() {
List<LitePalSupport> modelList = getModelList();
while (!modelList.isEmpty()) {
Random rand = new Random();
int index = rand.nextInt(modelList.size());
LitePalSupport model = modelList.remove(index);
model.save();
}
}
代码示例来源:origin: shekhargulati/99-problems
@Test
public void shouldWorkOnARandomArray() throws Exception {
int[] numbers = new Random().ints(100000, 1, 100000).toArray();
long start = System.currentTimeMillis();
int[] pairs = Problem01.twoSum(numbers, 100);
long end = System.currentTimeMillis();
System.out.println(String.format("Total time taken %d sec(s)", (end - start) / 1000));
System.out.println(Arrays.toString(pairs));
assertThat(pairs.length, equalTo(2));
}
代码示例来源:origin: stackoverflow.com
import objectexplorer.ObjectGraphMeasurer;
public class Measurer {
public static void main(String[] args) {
Set<Integer> hashset = new HashSet<Integer>();
Random rng = new Random();
int n = 10000;
for (int i = 1; i <= n; i++) {
hashset.add(random.nextInt());
}
System.out.println(ObjectGraphMeasurer.measure(hashset));
}
}
代码示例来源:origin: apache/storm
/**
* Creates a two task assignment by selecting random tasks.
*/
public int[] createAssignment(List<Integer> tasks, byte[] key) {
// It is necessary that this produce a deterministic assignment based on the key, so seed the Random from the key
final long seedForRandom = Arrays.hashCode(key);
final Random random = new Random(seedForRandom);
final int choice1 = random.nextInt(tasks.size());
int choice2 = random.nextInt(tasks.size());
// ensure that choice1 and choice2 are not the same task
choice2 = choice1 == choice2 ? (choice2 + 1) % tasks.size() : choice2;
return new int[]{ tasks.get(choice1), tasks.get(choice2) };
}
}
代码示例来源:origin: prestodb/presto
private List<SqlDecimal> createDecimalValuesNoNull()
{
Random random = new Random();
List<SqlDecimal> values = new ArrayList<>();
for (int i = 0; i < ROWS; ++i) {
values.add(new SqlDecimal(BigInteger.valueOf(random.nextLong() % 10000000000L), 10, 5));
}
return values;
}
}
内容来源于网络,如有侵权,请联系作者删除!