Random permutation of integers using a random numb

2020-02-07 13:01发布

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!

3条回答
\"骚年 ilove
2楼-- · 2020-02-07 13:40

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楼-- · 2020-02-07 13:45

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".

查看更多
Lonely孤独者°
4楼-- · 2020-02-07 13:46

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.

查看更多
登录 后发表回答