整数利用一个随机数发生器随机置换 [英] Random permutation of integers using a random number generator

查看:279
本文介绍了整数利用一个随机数发生器随机置换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的家庭作业:

 随机R =新的随机();
公众诠释get100RandomNumber(){
    返回1 + r.nextInt(100);
}
 

  

您将得到一个pre定义函数命名getrand100()(上图)这   返回一个整数,从1-100单随机数。您可以致电   只要你想,但要注意,这个函数这个函数多次   相当耗费资源。不能使用任何其他随机   发电机。您不可以更改的定义 getrand100()

     

输出:打印编号1-20的随机顺序。 (不是20的随机数)

我已经试过..

 公共类MyClass的{

    静态随机R =新随机();
    静态的HashSet<整数取代;

    公共静态无效的主要(字符串的args []){
        myMethod的();
        的System.out.println(多个);
    }

    公共静态无效在myMethod(){
        布尔B = FALSE;
        S =新的HashSet<整数GT;();
        INT I = getRand100();
        如果(ⅰ> = 20)
            I = I%20;
        INT J = 0;

        INT K,L;
        而(s.size()&其中; = 20)
        {
            的System.out.println(occurence没有+ + j)条;
            的System.out.println(发生值+ I);
            B = s.add(ⅰ);
            而(!B){
                K = + +我;
                如果(K&其中; = 20)
                    B = s.add(k)的;
                如果(二==真)
                    打破;
                如果(!B){
                    L = --i;
                    如果(ⅰ> = 1&安培;&安培; I&其中; = 20)
                        B = s.add(升);
                    如果(二==真)
                        打破;
                }
            }
        }
        的System.out.println(多个);
    }

    公共静态INT getRand100()
    {
        返回r.nextInt(100)+ 1;
    }
}
 

感谢您的帮助!

解决方案

我相信你是问如何使用一个随机数发生器,以随机顺序打印出的数字1到20。这也被称为随机排列。该费 - 耶茨洗牌是这样的算法。

不过,要实现该算法,你首先需要一个随机数生成器,可以挑选一个出来N个项目进行,其中N范围从2至设定洗牌的大小相等的概率,而你只有一个可以挑一个出来的100个项目的概率相同。可以很容易地通过模运算的组合来获得和再轧

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!

解决方案

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

这篇关于整数利用一个随机数发生器随机置换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆