0

Im doing a dxball like game for a school project. I am trying to create spawn points for the objects(blocks) that will move across the screen. But my problem is when i lets say create 2 objects of type Block. They will get the same random spawnlocation and they will keep having the same respawn location at all time. So i guess i am doing to random function wrong

public class blocks
{
    public Texture2D texturBlock1;
    public Vector2 hastighet = new Vector2(500.0f, 000.0f);
    public Vector2 position;
    private Random random = new Random();
    private int Screen = new int();




   public blocks(int MaxWith)
   {
       this.position.X = MaxWith+10;
       this.position.Y = random.Next(300);
       Screen = MaxWith;
   }

I would love if someone could help me out. I am a real novice when it comes to XNA and c#

colllin
  • 9,442
  • 9
  • 49
  • 65
Jemil Riahi
  • 1,360
  • 5
  • 18
  • 38
  • 1
    Don't forget to read FAQ on this question - http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number (also you already it mostly answered here too). – Alexei Levenkov Dec 09 '12 at 07:13

2 Answers2

5

The Random class in .NET usually uses System.DateTime as it's seed, so when you're creating multiple randoms at the same time you tend to get the same seed and generate the same random #.

Try out this static method which uses a GUID as the seed

public static int RandNumber(int low, int high)
{
        Random rndNum = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber));
        int rnd = rndNum.Next(low, high);
        return rnd;
}
Stan R.
  • 15,757
  • 4
  • 50
  • 58
  • Thanks alot for the fast answers. This one helped me out alot – Jemil Riahi Dec 09 '12 at 05:47
  • what's up with the downvote? I would like to know why this isn't a good method, because I use it in my code. – Stan R. Dec 09 '12 at 06:14
  • +1... also initializing with such a complicated seed for single static `Random` feels overkill. – Alexei Levenkov Dec 09 '12 at 07:11
  • @AlexeiLevenkov I tend to agree that it *feels* a bit overkill, however generating a GUID is fairly cheap and *almost guarantees* a unique value each time. – Stan R. Dec 09 '12 at 18:24
  • however i would like to point out that taking a substr of a GUID greatly increases the chance of collision..i.e. there is no guarantee that the first 8 bytes of consecutive GUID's are unique – Stan R. Dec 09 '12 at 18:33
  • Sorry Stan if i made a downvote. it was not my intention. You solution worked well for me. The cause of the downvote was because I was at school and the schools computers dont have any mousepads because people keep stealing them. So i clicked wrong by mistake because of the mouse tracktion – Jemil Riahi Dec 12 '12 at 08:30
  • don't worry about it, plus I don't think it was as the downvote is still there (you can unclick it, if it is yours) – Stan R. Dec 12 '12 at 15:27
3

The problem is probably that you create a new Random object for every block. That might initialise them all the same(give them the same seed for their pseudo-randomiser function), which results in them giving you the same random numbers.

The easiest way to get around this is simply making the private Random random static. Then there will be only one Random object for all blocks together, and they should all get different positions.

Hope this helps!

amulware
  • 412
  • 2
  • 15