簡體   English   中英

根據Java中的種子制作隨機整數數組

[英]Make an array of random integers based on seed in Java

我需要創建一個整數數組(雙精度和浮點數也很好,但我認為沒有區別),因為我需要對它們進行某些數學運算,例如*和+。 我試圖用隨機種子填充數組(即1337 5443,我需要使用它們),但是我無法將隨機變量轉換為int,也無法添加或乘以隨機變量。 因此,從本質上講,我需要根據特定種子制作一個隨機數數組,並且還需要能夠對列表中的每個元素進行數學運算。 這是到目前為止我所做的一個示例:

import java.util.Random;
public class{
public static int a [] = new int [101];
public static void main(String[] Args){
    for(int i = 0; i <= 100; i++){
        Random ran1 = new Random(1337);
        a [i] =  ran1;//THIS IS THE PROBLEM (incompatible types)
    }
    int sum = a[5] + a[8] * a[10];//this won't acctually be included, it's just an example
    }
}

你不分配Randomint -你需要調用nextInt ,傳遞int ,讓之間的范圍0和約束減1

a[i] = ran1.nextInt(10);  // 0-9 or substitute what you want for 10

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM