So, I'm making a simple game. It's like snake but the picturebox doesn't grow. In the game, you have to get fruits and other objects. Look at berry's code:

public class Berry : PictureBox
    public bool ON_SCREEN;
    public Berry()
        this.BackColor = Color.White;
        this.Size = new Size(20, 20);

And then I randomize it on a form. Like this:

public void Generator_Berry()
        Berry b = new Berry();
        Random randomY = new Random();
        int rY = randomY.Next(10, 270);
        Random randomX = new Random();
        int rX = randomX.Next(10, 270);
        b.Location = new Point(rX, rY);
        b.ON_SCREEN = true;

So the problem is that Berry's X position is the same as Y or vice-versa. I've never got a problem like this.(It look very simple, but I can't get the solution) What is wrong?! Am I that very stupid?!

  • 1
    \$\begingroup\$ Coding questions go on stackoverflow.com \$\endgroup\$
    – MichaelHouse
    Jun 7 '13 at 21:49
  • \$\begingroup\$ Byte56- I was, well, banned for asking "less important" or "low info" questions. That's why I use Gamedev. No offence, but, If it is dedicated to videogame development but not coding, then what should people ask? I think videogames have a lot to do with coding... \$\endgroup\$ Jun 7 '13 at 22:27
  • 4
    \$\begingroup\$ @user2080876: "I was, well, banned for asking 'less important' or 'low info' questions." That doesn't mean you should bring them here. And you were question-banned for asking bad questions, not "less important" ones. Byte56's main point is that this programming has little specifically to do with game development. \$\endgroup\$ Jun 7 '13 at 22:44
  • \$\begingroup\$ Good. thanks to a negative vote, i can't post questions anymore. I miss the old Internet. I can't ever thrust stack exchange websites. I hate these rules. \$\endgroup\$ Jun 12 '13 at 11:35

A pseudo-random number generator like C#'s System.Random generates a deterministic sequence of numbers from a starting state, a seed.

When you construct a System.Random instance without any arguments, it initializes the seed based on the current time. As your two instances are created very close in time, it's likely that they will get the same seed, and thus the same sequence of numbers.

It's common to create one instance, use it and hold on to it for any future generation of additional pseudo-random numbers in the sequence. Reseeding has a tendency to increase correlation, not reduce it.

Please avoid following knee-jerk responses like "never use more than one instance", those show a lack of understanding as to why you're experiencing the problems you do.

MSDN says:

The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers.

  • 2
    \$\begingroup\$ To expand on your point further: Random is not threadsafe - totally reasonable to create instances per-thread. \$\endgroup\$ Jun 8 '13 at 2:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.