在链表插入时生成两个范围内的随机数。

huangapple go评论54阅读模式
英文:

Generating random numbers between 2 ranges for Linked List insertion

问题

你好,以下是你要翻译的代码部分:

// 在两个范围 (-30,-10) 和 (10,30) 之间生成随机数,然后存储在链表节点中。
// 如果生成的数是负数,我们将该元素和下一个元素(无论其值如何)插入到列表的“头部”。
// 如果生成的数是正数,那么该元素和下一个元素将存储在“尾部”。
public class CAO_QUANG_JUIN_P4 {

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

        // 创建一个介于10和30之间的偶数N
        int N = (int)(Math.random() * 20) + 10;
        N = (N * 2) % 30;
        // 创建一个包含N个节点的简单链表
        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;
        }
    }
}

希望这对你有帮助!

英文:

Hi i am trying to generate a random number between 2 sets of range (-30,-10) and (10,30) to store in a LinkedList Node . If the number generated is negative, we insert this element and the next (regardless of its value) at the "head" of the list. If the number generated is positive then this element and the next will be stored at the 'tail'. This is what i have so far.

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;
	}

答案1

得分: 3

你有两个选择:

  1. 首先 '抛硬币' 来决定是正数还是负数,然后根据硬币翻转的结果生成 -10/-30 或 10/30 中的一个数字。
  2. 生成一个在 [0,40] 范围内的随机数,并将其映射到你实际需要的范围。

请注意,如果这两个范围不等大小,选项 #1 在想要均匀随机分布时会有问题。

作为一个独立的注意点,Math.random() * 20 是错误的。

从数学证明的角度来看:

  1. 在 Java 中,一个 double 类型有 64 位。这意味着它最多可以表示 2^64 个数字。这是很多数字,但并不是无限多的。
  2. ... 只有其中一些数字在 0(包括)和 1(不包括)之间。假设有 1000005 个这样的数字。
  3. 由于确切有 10000005 个这样的数字,并且每一个这 10000005 个数字都将 '映射' 到你的数字之一,而这些数字的总 '范围' 是 20(或 40,无所谓),嗯,40 不能完全匹配 10000005。
  4. 因此,我已经证明了某些数字会比其他数字出现得更频繁,因此你的结果并不是真正随机的。证毕。

解决方法是创建一个 Random 的实例,并使用其 .nextInt(20) 方法,这个方法确实是均匀分布的。

抛硬币方法

Random r = new Random(); // 在你的应用程序中创建一个实例,仅需一次。
boolean goNegative = r.nextBoolean();
int nr = (goNegative ? -1 : +1) * (10 + r.nextInt(20));
// 注意,与你的示例一样,10 是可能的,
// 但无法达到 30。如果需要,可以使用 r.nextInt(21)。

映射方法

Random r = new Random(); // 在你的应用程序中创建一个实例,仅需一次。
int nr = r.nextInt(40);
if (nr < 20) nr -= 29; // 包括了从 -29 到 -10 的所有数字。
else nr -= 10; // 包括了从 10 到 29 的所有数字。
英文:

You have two options:

  1. First 'flip a coin' to decide whether positive or negative, then, either generate -10/-30 or 10/30 depending on the coin flip.
  2. Generate a random number in the range [0,40] and map these onto your actual desired range.

Note that #1 is problematic if the 2 ranges aren't equally sized, if you want uniform random distribution.

As a separate note, Math.random() * 20 is wrong.

Fundamentally, by way of math proof:

  1. a double in java has 64 bits. That means it can represent at most 2^64 numbers. That's a LOT of numbers, but not an infinite amount of them.
  2. ... and only some of those numbers are between 0 (inclusive) and 1 (exclusive). Let's say it's 1000005 of them.
  3. Given that it's exactly 10000005 of them, and every single last one of those 10000005 numbers will 'map' to one of your numbers, which have a total 'range' of 20 (or 40, doesn't matter), well, 40 doesn't fit perfectly into 10000005.
  4. Therefore, I have proven that some numbers will occur more often than others, and your result is not truly random. QED.

The solution is to make an instance of Random and use its .nextInt(20) method which IS truly uniformly distributed.

Coin flip method

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.

Mapper method

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

答案2

得分: 2

尝试以下内容:

Random r = new Random();
// 以下生成一个介于0和20之间(包括0和20)的数字,并加上10
int a = r.nextInt(21) + 10; // 介于10和30之间(包括10和30)

// 以下执行相同操作,但改变了符号。
int b = -(r.nextInt(21) + 10); // 介于-10和-30之间(包括-10和-30)

// 对于负数,您也可以这样做。
b = -30 + r.nextInt(21);

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

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

Try the following:

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);

So if you want to randomly chose one from both sets you can do the following:

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

</details>



huangapple
  • 本文由 发表于 2020年8月2日 08:05:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63211163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定