Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
        this.Controls.Add(b);
        b.BringToFront();
        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?!

share|improve this question

closed as off topic by Byte56, Sean Middleditch, Maik Semder, Laurent Couvidou, Josh Petrie Jun 12 '13 at 15:15

Questions on Game Development Stack Exchange are expected to relate to game development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Coding questions go on stackoverflow.com –  Byte56 Jun 7 '13 at 21:49
    
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... –  user2080876 Jun 7 '13 at 22:27
4  
@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. –  Nicol Bolas Jun 7 '13 at 22:44
    
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. –  user2080876 Jun 12 '13 at 11:35

1 Answer 1

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.

share|improve this answer
    
Thanks. Awesome Information. –  user2080876 Jun 7 '13 at 22:25
2  
To expand on your point further: Random is not threadsafe - totally reasonable to create instances per-thread. –  Andrew Russell Jun 8 '13 at 2:33

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