Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do these random events follow a specific pattern?

Tags:

c#

random

uwp

I have created a grid layout. It displays 100 balls. Initially, all the balls will be in a random position. Every 25 ms a ball will move some unit (this is common for all the balls) in a random direction. You can see this in action in the below image:

Sample output in GIF

Even though the direction of the balls is random, after some time all the balls move towards the bottom right corner. This behavior repeats every time. You can see this in the below image:

Sample 1

Sample 2

  • Why do these random events follow a specific pattern?
  • Are random numbers truly random?
  • Is there is a problem with the C# random number generator?
  • Is there is any mathematical explanation for this?

C# Code

Random random = new Random();
var direction = random.NextDouble() * 360;
var ballTranslate = child.RenderTransform.TransformPoint(new Point(0, 0));
var x = ballTranslate.X;
var y = ballTranslate.Y;
var x1 = x + (parm.Speed * Math.Cos(direction));
while (x1 < 0 || x1 > (parm.CellSize * parm.NoOfSplit))
{
    direction = random.NextDouble() * 360;
    x1 = x + (parm.Speed * Math.Cos(direction));
}

var y1 = y + (parm.Speed * Math.Sin(direction));
while (y1 < 0 || y1 > (parm.CellSize * parm.NoOfSplit))
{
    direction = random.NextDouble() * 360;
    y1 = y + (parm.Speed * Math.Sin(direction));
}

TranslateTransform myTranslate = new TranslateTransform();
myTranslate.X = x1;
myTranslate.Y = y1;
child.RenderTransform = myTranslate;

Full Code

https://github.com/Vijay-Nirmal/ProjectChaos

like image 651
Vijay Nirmal Avatar asked Oct 15 '18 12:10

Vijay Nirmal


1 Answers

You appear to be generating a direction in degrees and passing it to Math.Sin, which takes an angle in radians. 360/2PI = 57.3 (approximately), so you're slightly more likely to pick an angle between 0 and 0.3 radians than other, larger angles.

When you have so many iterations, it's also possible that there's a tiny rounding error somewhere

like image 55
Robin Bennett Avatar answered Nov 01 '22 07:11

Robin Bennett