Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random values seem to be not really random?

Tags:

Trying to make a simple bacteria-killing game using WinForm in C#, but the bacteria (I am using Panel for the time being) doesn't seem to move around at random.

Specifically, the problem I am having is, the bacteria tries to move towards the upper left corner and move around there only. Ideally, the bacteria needs to move around the rectangle range evenly, but I am not sure how to achieve that. Look at the gif file below.

As you can see the red Panel moves around the upper left corner only. How can I get it to move everywhere evenly and randomly?

Here is my code:

private Panel _pnlBacteria;     //Panel representing a piece of bacteria private Random r = new Random();  //For randomly-generated values private int _prevX;        //Stores the previous X location private int _prevY;        //Stores the previous Y location  public Form1() {     InitializeComponent();     _pnlBacteria = new Panel();     /* Get more property assignments to this._pnlBacteria (omitted) */      //Bacteria's start position is also randomly selected     _prevX = r.Next(50, 300);     _prevY = r.Next(50, 500); }  //Timer runs every 100 seconds changing the location of the bacteria private void TmrMoveBacteria_Tick(object sender, EventArgs e) {     int x, y;     //Get random values for X and Y based on where the bacteria was previously     //and move randomly within ±10 range. Also it cannot go off the screen.     do      {                x = r.Next(_prevX - 10, _prevX + 10);         y = r.Next(_prevY - 10, _prevY + 10);     }      while ((y <= 0) || (y >= 500) || (x <= 0) || (x >= 300));      //Save the new location to be used in the next Tick round as previous values     _prevX = x;     _prevY = y;       //Apply the actual location change to the bacteria panel     _pnlBacteria.Top = y;     _pnlBacteria.Left = x; } 

I tried changing the +10 to +12, leaving -10 as it is, but now this only made the bacteria move to the bottom right corner only. I am at a loss. Can anyone please help?

like image 428
Jessica.D Avatar asked Aug 10 '18 19:08

Jessica.D


People also ask

Why are random numbers not actually random?

Pseudorandom Number Generation. Software-generated random numbers only are pseudorandom. They are not truly random because the computer uses an algorithm based on a distribution, and are not secure because they rely on deterministic, predictable algorithms.

Is random in programming really random?

Computers are often required to produce random numbers as they're useful for a host of tasks, from taking random samples of data to simulating the formation of galaxies. But computers produce these numbers using mathematical formulas, which means they aren't truly random.

Is there such a thing as truly random?

The short answer is no. By a definition of the word random in this context, it means that, in terms of cause and effect, an effect must occur without any cause. In a deterministic universe, this is impossible.

Why is true randomness impossible?

Randomness is relational. The problem modern computers have with randomness is that it doesn't make mathematical sense. You can't program a computer to produce true randomness—wherein no element has any consistent, rule-based relationship to any other element—because then it wouldn't be random.


1 Answers

If you read the documentation of Random.next(int,int) you'll find the the lower bound is inclusive and the upper bound is exclusive, that's why -10 and +11 works.

like image 184
Turo Avatar answered Oct 02 '22 22:10

Turo