Random permutation of integers using a random number generator

白昼怎懂夜的黑 提交于 2019-12-29 02:12:14

问题


This is my homework assignment:

Random r = new Random();
public int get100RandomNumber() {
    return 1+r.nextInt(100);
}

You are given a pre-defined function named getrand100() (above) which returns an integer which is one random number from 1-100. You can call this function as many times as you want but beware that this function is quite resource intensive. You cannot use any other random generator. You cannot change the definition of getrand100().

Output: Print numbers 1-20 in random order. (Not 20 random numbers)

What I have tried..

public class MyClass {

    static Random r = new Random();
    static HashSet<Integer>;

    public static void main(String args[]) {
        myMethod();
        System.out.println(s);
    }    

    public static void myMethod() {
        boolean b = false;
        s = new HashSet<Integer>();
        int i = getRand100();
        if (i >= 20)
            i = i % 20;
        int j = 0;

        int k, l;
        while (s.size() <= 20) 
        {
            System.out.println("occurence no" + ++j);
            System.out.println("occurence value" + i);
            b = s.add(i);
            while (!b) {
                k = ++i;
                if(k<=20)
                    b = s.add(k);
                if(b==true)
                    break;
                if (!b) {
                    l = --i;
                    if(i>=1&&i<=20)
                        b = s.add(l);
                    if(b==true)
                        break;
                }
            }
        }
        System.out.println(s);
    }

    public static int getRand100()
    {
        return r.nextInt(100) + 1;
    }
}

Thanks for any help!


回答1:


I believe you are asking how to use a random number generator to print out the numbers 1 to 20 in a random order. This is also known as a "random permutation". The Fischer-Yates shuffle is such an algorithm.

However, to implement the algorithm, you first of all need a random number generator that can pick one out of N items with equal probability where N ranges from 2 up to the size of the set to shuffle, while you only have one that can pick one out of 100 items with equal probability. That can easily be obtained by a combination of modulo arithmetic and "rerolling".




回答2:


Assuming you are allowed to use the ArrayList class, I'd recommend filling a list with the numbers you want (1 to 20 in this case), then randomly pick numbers from the list and remove them. Using getRand100() % theList.size() should be sufficiently random for your cause and you only need to call it 19 times. When only one element is left, there's no need to "randomly" pick it from the list anymore. ;-)




回答3:


I believe that I've come up with a way to convert any number between 1 and n! (assuming the number of items is known) to a unique permutation of n items.

In essence, this allows for an "immediate" randomization of an entire deck without having to use any shuffling algorithms. For now, it runs in O(n^2) and requires using BigInteger packages (ie. in Java or Javascript), but I'm looking for ways to optimize the runtime (although, honestly 2500 iterations is nothing these days anyway). Regardless, when given at least 226 bits of valid, random data, the function is able to generate a shuffled array of 52 integers in under 10 ms.

The method is similar to that used to convert a decimal number to binary (continually dividing by 2, etc). I'm happy to provide my code upon request; I find it interesting that I haven't come across it before.



来源:https://stackoverflow.com/questions/10872132/random-permutation-of-integers-using-a-random-number-generator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!