0

I am very new to c# and I am trying to create a sandbox type game, I am using random numbers to pick what blocks go where, but my blocks are always the same because it is always picking the same 'random number'. Here's My Code:

    int x = 0; 

    public GameWindow() 
    {
        InitializeComponent();
        Blocks();
    }

    private void BlockThree()
    {

    }

    private void BlockTwo()
    {
        x = 2;
        BlockData();
    }

    private void BlockOne()
    {
        x = 1;
        BlockData();
    }

    private async void Blocks()
    {
        await Task.Delay(5000);
        BlockOne();
        await Task.Delay(5000);
        BlockTwo();
        await Task.Delay(5000);
        BlockThree();
    }

    private async void BlockData()
    {
        Random rand = new Random();

        int num = rand.Next(1, 2);

        if (num == 1)
        {
            if (x == 1)
            {
                pictureBox1.Image = Image.FromFile("C:/Program Files (x86)/SandBoxEngine/grass_side.png");
            }
            else
            {
                if (x == 2)
                {
                    pictureBox2.Image = Image.FromFile("C:/Program Files (x86)/SandBoxEngine/grass_side.png");
                }
            }
        }
        else
        {
            if (num == 2)
            {
                if (x == 1)
                {
                    pictureBox1.Image = Image.FromFile("C:/Program Files (x86)/SandBoxEngine/dirt.png");
                }
                else
                {
                    if (x == 2)
                    {
                        pictureBox2.Image = Image.FromFile("C:/Program Files (x86)/SandBoxEngine/dirt.png");
                    }
                }
            }
        }   
    }
}
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • 1
    Only use 1 `Random` instance, otherwise you'll almost always get the same random number. – Styxxy Nov 09 '13 at 19:48
  • 2
    (There are loads of duplicates of this, basically.) Also see http://csharpindepth.com/Articles/Chapter12/Random.aspx – Jon Skeet Nov 09 '13 at 19:49
  • If you create multiple instances of `Random` withing a short timeframe, they will all return the same sequence. Generate one and store it in a field. When using multi-threading don't forget to use proper locking, `Random` isn't thread-safe. – CodesInChaos Nov 09 '13 at 19:51

1 Answers1

3

You need to specify the Random as a global.

    private Random rand = new Random();

then use the rand.Next

Also

    rand.Next(1,2);

will always return 1. to get 1 or 2 use this.

    rand.Next(1,3);
deathismyfriend
  • 2,182
  • 2
  • 18
  • 25
  • You will also find the low bit tends to go 0,1,0,1,0,1 (at least it used to). – David Thielen Nov 09 '13 at 19:55
  • Thanks For That, It still doesnt seem to be working also making the random global gives me an error. – user2974743 Nov 09 '13 at 22:00
  • Can you show how you are making it global and what the error is ? – deathismyfriend Nov 09 '13 at 22:21
  • I think it is connected to the seed value, since Random is a pseudorandom generator, so try something like: Random r = new Random(DateTime.Now.Millisecond); – David Venegoni Nov 10 '13 at 06:08
  • 1
    @DavidVenegoni, the parameterless constructor for [Random](http://msdn.microsoft.com/en-us/library/system.random.aspx), "initializes a new instance of the Random class, using a time-dependent default seed value," so seeding with DateTime.Now.Millisecond probably won't help. Also, since there are only 1000 milliseconds in a second, this will limit the possible seeds to a value in [0, 999] instead of the possible [0, Int32.MaxValue], hurting the effective randomness quite a lot. – chwarr Nov 10 '13 at 10:16
  • 1
    @c45207 You are correct, I consulted MSDN, and it making it global and using the default parameterless constructor is the correct solution. Apologize for my initial comment, thank you for clarifying my mistake. – David Venegoni Nov 10 '13 at 22:22