Reputation: 1
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");
}
}
}
}
}
}
Upvotes: 0
Views: 242
Reputation: 2192
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);
Upvotes: 3