簡體   English   中英

Java - 生成隨機數

[英]Java - Generating random numbers

我需要從 1 -100 生成一個隨機數,並在一行和 10 行中組織 10 個數字。

我不知道為什么數字不是隨機的。

import java.util.Random;


class Main {
    public static void main(String[] args) {
        int array[];
        array = new int[100];


        for (int i = 0; i < 100; i++) {
            Random rd = new Random();
            array[i] = rd.nextInt(100)+1;
            array[i] = i+1;
            System.out.print(array[i] + " ");
            if(array[i]%10==0)
                System.out.println();
        }
    }
}

你的代碼有兩個問題:

  1. Random rd = new Random(); 應該移出循環
  2. array[i] = i+1; 應該刪除

我認為您的代碼段應如下所示:

public static void main(String[] args) {
    int[][] res = generateRandom(10);

    for (int i = 0; i < res.length; i++)
        System.out.println(Arrays.stream(res[i])
                                 .mapToObj(n -> String.format("%3d", n))
                                 .collect(Collectors.joining(" ")));
}

public static int[][] generateRandom(size) {
    int[][] res = new int[size][size];
    Random random = new Random();

    for (int row = 0; row < res.length; row++)
        for (int col = 0; col < res[row].length; col++)
            res[row][col] = random.nextInt(100) + 1;

    return res;
}

輸出:

 69  71  77  25  85  26  18  11  26  44
 78  70  90  46  78  66  40   4  84  19
 86  20  60   1  66  19  93  44  43  12
  9  26   1  22  39  79  10  76  39  80
 51  16  92  36  90  58   2  63  13  54
 18  20  36  90  27  78  97  33  64  15
 61  61  20  54  56  20  18  59  58  50
 85  33   4  68  89  36  34  96  97  77
 67  14   8  60  95  99  50  73  86  89
 48  75  25  63 100  81  82  59  60  57

您需要刪除以下行以生成隨機數。
數組[i] = i+1;

只需刪除

array[i] = i+1;

因為它覆蓋了隨機值。

//in your code, you basically getting unintended results from;

//your generated random is being overwritten
array[i] = rd.nextInt(100)+1;
array[i] = i+1;

// your condition for moving to the next line is based on the value not the index
if(array[i]%10==0)
enter code here`System.out.println();


import java.util.Random;

class Main {
    public static void main(String[] args) {
        int array[];
        array = new int[100];


        for (int i = 0; i < 100; i++) {
            Random rd = new Random();

            //random number generation and assigning to array
            array[i] = rd.nextInt(100)+1;

            //showing random number
            System.out.print(array[i]);

            //space management
            if(array[i] < 10)
                System.out.print("   ");
            else if(array[i] < 100)
                System.out.print("  ");
            else
                System.out.print(" ");

            //to organize as intended
            if((i+1) % 10 == 0)
                System.out.println();

        }
    }
}

您的代碼有兩個問題:

  1. 您正在使用語句array[i] = i+1;覆蓋存儲在array[i]的隨機數array[i] = i+1; . 您需要刪除此語句。
  2. 條件if(array[i]%10==0)不會在每10數字后給您換行,因為您正在檢查存儲在數組中的值而不是循環計數器。 應該是if ((i + 1) % 10 == 0)

此外,您應該在循環之外初始化Random

注意:您所做的方式永遠不會給您唯一的隨機數。 請注意,您的語句rd.nextInt(100)+1將為您提供一個從1100的整數,但是當您重復執行此語句時,您可能會多次得到相同的數字。

我能想到的最簡單和最快的方法是用從1100整數初始化一個列表,隨機化列表並在 10x10 表中打印列表,如下所示:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();

        // Fill the list with integers from 1 to 100
        for (int i = 1; i <= 100; i++) {
            list.add(i);
        }

        // Shuffle the numbers in the list
        Collections.shuffle(list);

        // Display in a 10x10 table
        for (int i = 0; i < 100; i++) {
            System.out.printf("%4d", list.get(i));
            if ((i + 1) % 10 == 0) {
                System.out.println();
            }
        }
    }
}

示例運行:

  44  79  58 100  34  24  20  12   6  90
  80  57  85  88  30  60  16  56  43  42
  45  92  33  50  28  22  13  66  40  97
   5  54  71  48  94  86  99  10  76  27
  55  95  36   9  77   7  78  69  67  31
  82  21  17  96   2  47   3  74  63  29
  73   8  14  93  75  49  41  81  61  68
  23  15  38  64  52  18  32  89  84  11
  19  72  62  26  46  65  70   4  53   1
  98  37  39  91  51  25  35  87  59  83

暫無
暫無

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

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