简体   繁体   中英

Java - Generating random numbers

I need to generate a random number from 1 -100 and organize 10 numbers in a line and 10 lines.

I don't know why the numbers aren't random.

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();
        }
    }
}

You have two problems in the code:

  1. Random rd = new Random(); should be moved out of the loop
  2. array[i] = i+1; should be removed

I think your snippet should look like this:

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;
}

Output:

 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

you need to remove the below line to generate random numbers.
array[i] = i+1;

Just remove

array[i] = i+1;

Because it override the random values.

//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();

        }
    }
}

There are two issues with your code:

  1. You are overriding the random number stored in array[i] with the statement, array[i] = i+1; . You need to remove this statement.
  2. The condition, if(array[i]%10==0) will not give you line break after every 10 numbers because you are checking the value stored in the array instead of the loop counter. It should be if ((i + 1) % 10 == 0) .

Also, you should initialize Random outside of the loop.

Note: The way you have done will never give you unique random numbers. Note that your statement, rd.nextInt(100)+1 will give you an integer from 1 to 100 but when you execute this statement repeatedly, you may get same numbers repeated many times.

The easiest and fastest way I can think of is to initialize a list with integers from 1 to 100 , randomize the list and print the list in a 10x10 table as shown below:

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();
            }
        }
    }
}

A sample run:

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM