Explanation for an interesting phenomenon using Random () and colors

Well, my student had a cool result that I simply could not explain. In her code, she wants to create random circles (in WPF) with random colors. She made the typical mistake of starting out creating more than one random number generator, but naked with me, please.

This is the code:

Random cir = new Random(); Random color = new Random(); for (i = 0; i < 100; i++) { int r = cir.Next(0, 50); diameter = r * 2; int posx = cir.Next(0, (510 - diameter)); int posy = cir.Next(0, (280 - diameter )); byte c1 = (byte)color.Next(255); byte c2 = (byte)color.Next(255); byte c3 = (byte)color.Next(255); SolidColorBrush usedcolor = new SolidColorBrush(Color.FromRgb(c1,c2,c3 )); Ellipse circle = new Ellipse(); circle.Height = diameter; circle.Width = diameter; circle.Margin = new Thickness(posx, posy, 0, 0); circle.Stroke = usedcolor; // melkweg.StrokeThickness = dikte; Ruimte.Children.Add(circle); } 

This will always generate the same effect: the colors look “grouped”, you will always get the same colors in the same region. Two examples: enter image description here

I understand that two random generators are created in the quasi-exact same moment and therefore have the same seed and are basically “synchronized”. However, since we require different ranges each time, I thought this would lead to uncorrelated numbers, but I'm clearly mistaken. (Range does not affect? ​​Does Random.Next always produce a number from 0 to 1, and then expand it (denormalise?) To the desired range?)

Even with a simple call to another Next () parameter in a circus or color (for example, color.Next (1);) it generates a completely random shape at will, and not a cold, but undesirable effect.

So, my main question is: why are these colors “grouped” and what aspect of random number generation and “synchronized” generators do I miss?

Hope this explanation is complete and someone can help me. Thanks a bunch!

PS Here the desired result (using 1 Random or adding a useless Next () - call) enter image description here

+5
source share
1 answer

So, Random.Next(int min, int max) works something like this:

  • Generate a random integer in the whole integer range
  • A card that doubles in the range of 0 .. 1.
  • Double this to the range (double * (max - min)) + min

This means that if you have two random houses with the same seed, parallel calls to them will create absolutely correlated values, no matter what ranges you use.

 var r1 = new Random(2); var r2 = new Random(2); Console.WriteLine(r1.Next(0, 10)); // 7 Console.WriteLine(r1.Next(0, 10)); // 4 Console.WriteLine(r1.Next(0, 10)); // 1 Console.WriteLine(r2.Next(0, 20)); // 15 Console.WriteLine(r2.Next(0, 20)); // 8 Console.WriteLine(r2.Next(0, 20)); // 3 

In your example, you have two randoms with the same seed, and you call each of them exactly 3 times in each iteration of the loop. What effect does this have?

  • The radius and the R part of the color have a strong correlation.
  • The X-coordinate and G-part of the color have a strong correlation (when X is large - G is also large and vice versa).
  • The Y coordinate and color B have a strong correlation. (whenever Y is big, B is also big, and vice versa).

From this, I think it’s obvious why the circles are grouped by color (parts G and B).

Whenever you add another call to Random.Next to any of the randoms - it breaks the perfect correlation of 3 + 3 calls, so the effect disappears (although if you make Random.Next calls for both randoms, you will get the same effect again) .

+7
source

Source: https://habr.com/ru/post/1260635/


All Articles