java
随机正态分布、随机整数
import java.util.Random;
class MyRandom {
static Random random = new Random();
static int nextInt(int bound) {
return random.nextInt(bound);
}
static double nextDouble() {
return random.nextDouble();
}
static double nextGaussian() {
return random.nextGaussian();
}
}
随机数
class MyRandom {
static int randomInt(int bound) {
return (int) (Math.random() * bound);
}
static double random() {
return Math.random();
}
}
随机睡眠
class RandomSleep {
static int randomInt(int start, int end) {
return (int) (start + Math.random() * (end - start));
}
static int randomInt(int end) {
return (int) (Math.random() * end);
}
static double random() {
return Math.random();
}
static void sleep(long millisecond) {
try {
Thread.sleep(millisecond);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static void sleepRandomly(int end) {
try {
Thread.sleep(randomInt(end));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Python
随机数
import random
for _ in range(9):
print(random.random())
for _ in range(9):
print(random.randint(1, 5))
import numpy
a = numpy.random.normal()
print(a)
随机数组
numpy.random.normal(size=(2, 4))
numpy.random.uniform(low=0.0, high=1.0, size=(2, 4))
随机种子
import numpy
numpy.random.seed(2)
print(numpy.random.rand())
Scala
val random: Random = new Random
println(random.nextInt(5))
println(random.nextGaussian())