我有這樣的代碼:矩陣中的隨機值,如何以給定的概率設置它們?
import java.util.Random;
public class Vectors {
public static int[][] vectors() {
return vectors(200,150,12345);
}
// the function creates an array of vectors
// size is the number of vectors
// dim is the dimension
// seed is for the random number generator
//
public static int[][] vectors(int size, int dimension, int seed) {
Random rng = new Random(seed);
int[][] answer = new int[size][dimension];
for(int i=0; i<size; i++) {
for(int j=0; j<dimension; j++) {
answer[i][j] = rng.nextInt(20) - 10;
}
}
return answer;
}
}
我具有由150行構建50列的隨機矩陣M。矩陣中的值爲{-1/√ 50, 1/√ 50}
,每個值的概率爲50%。
我如何通過幫助下面的代碼來做到這一點?
我的家庭作業感是刺痛的。 – christopher
我的太..有時它讓我咬。 – Maroun