本文整理了Java中java.util.Random.nextInt()
方法的一些代码示例,展示了Random.nextInt()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Random.nextInt()
方法的具体详情如下:
包路径:java.util.Random
类名称:Random
方法名:nextInt
[英]Returns a pseudo-random uniformly distributed int.
[中]返回伪随机均匀分布整数。
代码示例来源:origin: hankcs/HanLP
public static <T> void shuffleArray(T[] ar)
{
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
T a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
代码示例来源:origin: apache/rocketmq
@Override
public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
int value = random.nextInt(mqs.size());
return mqs.get(value);
}
}
代码示例来源: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: apache/incubator-druid
private void generateVals(final int totalSize, final int maxValue)
{
vals = new ArrayList<>(totalSize);
for (int i = 0; i < totalSize; ++i) {
int len = rand.nextInt(2) + 1;
int[] subVals = new int[len];
for (int j = 0; j < len; ++j) {
subVals[j] = rand.nextInt(maxValue);
}
vals.add(subVals);
}
}
代码示例来源: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: google/guava
private static <T> List<T> shuffledCopy(List<T> in, Random random) {
List<T> mutable = newArrayList(in);
List<T> out = newArrayList();
while (!mutable.isEmpty()) {
out.add(mutable.remove(random.nextInt(mutable.size())));
}
return out;
}
}
代码示例来源: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
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: 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: 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: 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: springside/springside4
/**
* 从输入list中随机返回随机个对象.
*/
public static <T> List<T> randomSome(List<T> list) {
int size = random.nextInt(list.size());
if (size == 0) {
size = 1;
}
return randomSome(list, size);
}
}
代码示例来源:origin: alibaba/arthas
public void run() throws InterruptedException {
try {
int number = random.nextInt()/10000;
List<Integer> primeFactors = primeFactors(number);
print(number, primeFactors);
} catch (Exception e) {
System.out.println(String.format("illegalArgumentCount:%3d, ", illegalArgumentCount) + e.getMessage());
}
}
代码示例来源:origin: apache/storm
public void execute(Tuple input, BasicOutputCollector collector) {
total = total.add(new BigInteger(input.getValues().get(1).toString()));
collector.emit(tuple(total.toString()));
//prints the total with low probability.
if(RANDOM.nextInt(1000) > 995) {
LOG.info("Running total = " + total);
}
}
代码示例来源:origin: neo4j/neo4j
@Test
public void shouldLoadProcedureFromJarWithSpacesInFilename() throws Throwable
{
// Given
URL jar = new JarBuilder().createJarFor( tmpdir.newFile( new Random().nextInt() + " some spaces in filename.jar" ),
ClassWithOneProcedure.class);
// When
List<CallableProcedure> procedures = jarloader.loadProceduresFromDir( parentDir( jar ) ).procedures();
// Then
List<ProcedureSignature> signatures = procedures.stream().map( CallableProcedure::signature ).collect( toList() );
assertThat( signatures,
contains( procedureSignature( "org", "neo4j", "kernel", "impl", "proc", "myProcedure" ).out( "someNumber", NTInteger ).build() ) );
assertThat( asList( procedures.get( 0 ).apply( new BasicContext(), new Object[0], resourceTracker ) ),
contains( IsEqual.equalTo( new Object[]{1337L} ) ) );
}
代码示例来源: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: apache/incubator-druid
@Setup
public void setup()
{
Random rand = ThreadLocalRandom.current();
sequences = new ArrayList<>();
for (int i = 0; i < count; i++) {
int[] sequence = new int[sequenceLength];
for (int j = 0; j < sequenceLength; j++) {
sequence[j] = rand.nextInt();
}
sequences.add(Sequences.simple(Ints.asList(sequence)));
}
}
代码示例来源:origin: ben-manes/caffeine
@Override Node select(List<Node> sample, Random random, long tick) {
int victim = random.nextInt(sample.size());
return sample.get(victim);
}
},
代码示例来源:origin: google/guava
private static void runLeastOfComparison(int iterations, int elements, int seeds) {
Random random = new Random(42);
Ordering<Integer> ordering = Ordering.natural();
for (int i = 0; i < iterations; i++) {
List<Integer> list = Lists.newArrayList();
for (int j = 0; j < elements; j++) {
list.add(random.nextInt(10 * i + j + 1));
}
for (int seed = 1; seed < seeds; seed++) {
int k = random.nextInt(10 * seed);
assertEquals(ordering.sortedCopy(list).subList(0, k), ordering.leastOf(list, k));
}
}
}
代码示例来源:origin: stackoverflow.com
import java.util.Random;
//...
Random r = new Random();
String alphabet = "123xyz";
for (int i = 0; i < 50; i++) {
System.out.println(alphabet.charAt(r.nextInt(alphabet.length())));
} // prints 50 random characters from alphabet
内容来源于网络,如有侵权,请联系作者删除!