【问题标题】:Generating random numbers between 2 ranges for Linked List insertion为链表插入在 2 个范围之间生成随机数
【发布时间】:2020-08-02 00:05:56
【问题描述】:

您好,我正在尝试在 2 组范围 (-30,-10) 和 (10,30) 之间生成一个随机数,以存储在 LinkedList 节点中。如果生成的数字是负数,我们将这个元素和下一个元素(不管它的值如何)插入到列表的“头部”。如果生成的数字是正数,那么这个元素和下一个元素将存储在“尾部”。这是我目前所拥有的。

public class CAO_QUANG_JUIN_P4 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    //////////////////////////bloc1:Decleration des variables//////////////////////////

    //Create a EVEN N variable between 10 and 30
    
    int N = (int)(Math.random()*20)+10;
    N = (N*2)%30;
    //Create a simple linkedlist with N nodes
    LC e1 = new LC();
    LC tete = null;
    while(N!=0) {
        if(e1==null) {
            e1 = new LC();
            tete = e1;
            e1.data = N;
            
        }
        else {
            e1.suiv = new LC();
            e1.suiv.data = N;
            e1 = e1.suiv;
        }
        N = (int)(Math.random()*20)+10;
        int M = (int)(Math.random()*-20)-10;
    }

【问题讨论】:

  • 问题是什么?
  • @hev1 我必须在 (-10,-30)(10,30) 之间生成随机数才能将其放入链接列表中。如果它是负数,它将存储在列表的头部,如果它是正数,它将存储在尾部
  • @QuangCao 这不是问题,你只要说出你想做什么。您需要制定一个我们可以回答的问题。例如:“这是我的问题 [...],这是我的解决方案 [...]。它不起作用,因为 [...]。我该如何解决?”
  • 抱歉,英语不是我的第一语言,我正在翻译我的任务。我的问题是如何在 (-10,-30) 和 (10,30) 之间创建一个随机数,因为它是 2 个不同的集合
  • @QuangCao 您希望数字包含限制还是介于两者之间?我不确定您使用的是 () 还是 [] 表示法。

标签: java singly-linked-list


【解决方案1】:

你有两个选择:

  1. 首先“抛硬币”来决定是正面还是负面,然后根据抛硬币的情况生成 -10/-30 或 10/30。
  2. 在 [0,40] 范围内生成一个随机数,并将其映射到您实际所需的范围内。

请注意,如果 2 个范围的大小不相等,如果您想要均匀的随机分布,则 #1 会出现问题。

作为单独的说明,Math.random() * 20 是错误的。

基本上,通过数学证明:

  1. java 中的double 有 64 位。这意味着它最多可以表示 2^64 个数字。这是很多数字,但不是无限的。
  2. ...只有其中一些数字介于 0(含)和 1(不含)之间。假设有 1000005 个。
  3. 鉴于正好是 10000005 个,并且这 10000005 个数字中的每一个最后一个数字都会“映射”到您的一个数字,它们的总“范围”为 20(或 40,没关系),好吧, 40 并不完全适合 10000005。
  4. 因此,我已经证明某些数字会比其他数字更频繁地出现,而您的结果并不是真正随机的。 QED。

解决方案是创建一个 Random 实例并使用其.nextInt(20) 方法,IS 真正均匀分布。

抛硬币法

Random r = new Random(); // make one instance, once, in your app.
boolean goNegative = r.nextBoolean();
int nr = (goNegative ? -1 : +1) * (10 + r.nextInt(20));
// note, like in your example, 10 is possible,
//but 30 cannot be hit. Make it r.nextInt(21)
//instead if it needs to be.

映射方法

Random r = new Random(); // make one instance, once, in your app.
int nr = r.nextInt(40);
if (nr < 20) nr -= 29; // all numbers from -29 to -10, inclusive, covered.
else nr -= 10; // covers 10-29 inclusive.

【讨论】:

  • 谢谢!它帮助很大!现在要弄清楚如何将它们插入头或尾节点..
【解决方案2】:

尝试以下方法:

Random r = new Random();
// the following generates a number between 0 and 20 inclusive and adds 10 to it
int a = r.nextInt(21)+10; // between 10 and 30 inclusive

// the following does the same but changes the sign.
int b = -(r.nextInt(21)+10); // between -10 and -30 inclusive

For the negative one you could also do this.
b = -30+r.nextInt(21);

因此,如果您想从两组中随机选择一个,您可以执行以下操作:

int n = nextInt(2) == 0 ? -30+r.nextInt(21) : r.nextInt(21)+10;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    相关资源
    最近更新 更多