创建一个具有特定范围内随机值的数组

时间:2013-12-28 17:34:22

标签: java arrays random

大家!

我的任务是创建一个小程序,为彩票生成不同的赌注。我有几个小时想知道应该在每次下注中创建具有不同数字的数组的代码片段。问题在于,尽管我试图减少重复,但我的代码仍然在矩阵的每一行内重复数字生成投注。 您认为我应该更深入地看待什么?

public static void main(String[] args) {


    //Ask the user how many lines of 15 numbers will be created
    System.out.print("How many lines should the matrix have? ");
    Scanner input = new Scanner(System.in);
    int numberOfLines = input.nextInt();


    //  Create the two-dimension array.
    int[][] numbers = new int[15][numberOfLines];
    for (int i = 0; i < numberOfLines; i++) {
        for (int j = 0; j < 15; j++) {
            boolean exist = false;
            do {
                numbers[j][i] = 1 + (int)(Math.random()*25);
                for (int k = 0; k < j; k++) {
                    if (numbers[j][i] == numbers[k][i])
                        exist = true;
                    else {
                        exist = false;
                    }
                }               
            } while (exist);
        }
    }

    //Sort the array
    int temp = 0;
    for (int i = 0; i < numberOfLines; i++){
        for (int j = 0; j < 15; j++) {
            for (int k = (j + 1); k < 15; k++)
                if (numbers[k][i] > numbers[j][i]) {
                    temp = numbers[j][i];
                    numbers[j][i] = numbers[k][i];
                    numbers[k][i] =  temp;
                }
        }
    }


    //Print the array
    System.out.println();
    for(int i = 0; i < numberOfLines; i++) {
        System.out.printf("Line %d:  ", (i+1));
        for (int j = 0; j < 15; j++){
            System.out.printf("%4d",numbers[j][i]);
        }
        System.out.println();
        System.out.println();

    }
    //System.out.println(numbers[0][1]);
    //System.out.println(numbers[1][0]);
}

}

2 个答案:

答案 0 :(得分:1)

如果您的主要问题是从较大的集合中选择一组没有重复的数字,那么这是一个简单的示例类。每次调用sample时,都会对列表进行洗牌,并返回第一个n项。

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

class Lotto {
    private List<Integer> nums = new ArrayList<Integer>();

    // constructor
    public Lotto(int m) {
        // add m numbers to list
        for (int i=1; i <= m; ++i)
            nums.add(i);
    }

    public List<Integer> sample(int n) {
        // Randomly samples n integers from 1 to m without duplicates
        Collections.shuffle(nums);
        return nums.subList(0, 6);
    }
}

以下是DrJava's互动标签中使用的类的示例:

> Lotto from40 = new Lotto(40)
> from40.sample(6)
[37, 2, 24, 30, 19, 29]
> from40.sample(6)
[17, 8, 16, 25, 33, 39]

答案 1 :(得分:0)

做这样的事情怎么样:

int[] createRanomArray(int size, int lowBound, int highBound) {
    Random random = new Random();

    Set<Integer> randomsSet = new HashSet<Integer>(size);

    while (randomsSet.size() < size) {
        randomsSet.add(lowBound + random.nextInt(highBound - lowBound));
    }

    List<Integer> asList = new ArrayList<Integer>(randomsSet);
    Collections.shuffle(asList);

    int[] result = new int[size];
    for (int i = 0; i < size; i++) {
        result[i] = asList.get(i);
    }
    return result;
}

现在asList列表包含一组随机值,​​现在只需将每个值分配给矩阵中列的单元格。

Set不接受重复值,因此可以确保不会出现重复结果,并且shuffle会使项目不符合哈希码顺序。