What's new

.NET The Random class not being Random

12ice

12ice

Windows Phone App Developer
Messages
543
Reaction score
66
Points
95
Sin$
0
Ok so I have an XNA game that has a food object class, inside of the class there is some logic to generate a random spawn within some parameters for the object. However when the game first starts, It instantiates these objects virtually simultaneously (There is a spawn loop until a required amount of objects have spawned) that looks like this:

Code:
if (gameTimer < 60000)
			{
 
				if (totalFoodObjects < 1)
				{
					int foodPicker = new Random().Next(1, 20);
 
					if (foodPicker <= 20)
					{
						baconList.Add(new Bacon());
						Bacon lastBacon = baconList.Last();
						lastBacon.setup();
						AddFoodObjects();
					}
				}
			}


Inside of the objects class is this logic:

Code:
  public void getStartingPosition()
		{
		   
	  //Thread this
			 
			Random random = new Random();
			int randomX = random.Next(400, 800);
		   
			if (randomX <= 800)
			{
				int randomY = random.Next(-50, 300);
				food1Position.Y = randomY;
				food1Position.X = randomX;
				subtractXAxis = -6f;
				food1Speed.Y = food1Speed.Y + 0.1f;
				food1Speed.X = subtractXAxis;
				return;
			}
			....... More else If statements like this
 
 
 
 
}


The problem being that because the Objects are calling the random class within less than milliseconds of each other Random returns the same X and Y "Random" values for each objects making the spawns not random at all. I know this is because the Random class is Pseudo random and is basing the random value off something somewhere but am I doing anything wrong ? and what can I do (Apart from pre determine the spawns)
 
Liquid44

Liquid44

Banned
Programmer
Messages
1,158
Reaction score
691
Points
245
Sin$
0
Code:
  public void getStartingPosition()
		{
	   
	  //Thread this
 
			Random random = new Random();
			int randomX = random.Next(400, 800);


I would implement my own random number generator, I suggest using Sony's technique that they used for their elliptic curve cryptography in the ps3 it is truly advanced...

Code:
int getRandomNumber()
{
  return 4;//chosen by fair dice roll
		  //guaranteed to be random
}

lol on a serious note, you are instantiating a new Random class every time you want to get a random position that's why you get the same values. Im assuming the random class is based on an internal seed and so you only need one instance and keep using the Next method on a single Random instance.
 
12ice

12ice

Windows Phone App Developer
Messages
543
Reaction score
66
Points
95
Sin$
0

Elliptic Curve Cryptography ahahaha, #NextGen
but yeah just make a method in my main game class that loads the Random variable at the start and then call that from my classes. Thanks! Didn't know about that instantiation bug thing :/
 
ActionScript

ActionScript

XG R4PiDzZ
Grizzled Veteran Programmer Modder
Messages
2,649
Reaction score
1,405
Points
475
Sin$
0
Elliptic Curve Cartography ahahaha, #NextGen
but yeah just make a method in my main game class that loads the Random variable at the start and then call that from my classes. Thanks! Didn't know about that instantiation bug thing :/

It's cryptography and also a thing.
 
Jakes625

Jakes625

Enthusiast
Messages
58
Reaction score
21
Points
55
Sin$
7
Ok so I have an XNA game that has a food object class, inside of the class there is some logic to generate a random spawn within some parameters for the object. However when the game first starts, It instantiates these objects virtually simultaneously (There is a spawn loop until a required amount of objects have spawned) that looks like this:

Code:
if (gameTimer < 60000)
			{
 
				if (totalFoodObjects < 1)
				{
					int foodPicker = new Random().Next(1, 20);
 
					if (foodPicker <= 20)
					{
						baconList.Add(new Bacon());
						Bacon lastBacon = baconList.Last();
						lastBacon.setup();
						AddFoodObjects();
					}
				}
			}


Inside of the objects class is this logic:

Code:
  public void getStartingPosition()
		{
		 
	  //Thread this
 
			Random random = new Random();
			int randomX = random.Next(400, 800);
		 
			if (randomX <= 800)
			{
				int randomY = random.Next(-50, 300);
				food1Position.Y = randomY;
				food1Position.X = randomX;
				subtractXAxis = -6f;
				food1Speed.Y = food1Speed.Y + 0.1f;
				food1Speed.X = subtractXAxis;
				return;
			}
			....... More else If statements like this
 
 
 
 
}


The problem being that because the Objects are calling the random class within less than milliseconds of each other Random returns the same X and Y "Random" values for each objects making the spawns not random at all. I know this is because the Random class is Pseudo random and is basing the random value off something somewhere but am I doing anything wrong ? and what can I do (Apart from pre determine the spawns)


don't listen to the idiots below. They have no idea what they're talking about...

The reason the numbers are similar is because it is just a PRNG, and uses a base of time as a way to calculate randomness. If you want them to be more random move the 'Random' object outside of the for loop.

ie:
Code:
Random r = new Random();
for(int i=0; i<10; i++){
	list1.Add( r.Next( min, max ) );
}

Also I will suggest to use cryptographic random numbers. They are much more random.
(this is not referring to ecsa, i have no idea what those idiots are talking about. ecdsa is a signature mechanism to assure validity)
 
12ice

12ice

Windows Phone App Developer
Messages
543
Reaction score
66
Points
95
Sin$
0


Sorry dude I don't instantiate the Random variable in a loop. I would be a pretty bad programmer if I did haha. I did try and explain this in my post but its hard to explain. The Random is in a class, Which is a food object in my game. I instantiate(spawn) lots of these objects virtually simultaneously at the start of the game and I need to randomize the positions where they spawn but they always return me the same value. (The variable is declared when I instantiate the class)

Would like to know more about other methods of getting Random numbers though, to ActionScript suggesting these over complicated methods. I don't think these Elliptic Curve Cryptographic methods are really going to be feasible in my game that is designed for mobiles. I just need a simple random integer generator.

P.s Although credit where it is due I think Liquid44 hit the nail on the head with

"lol on a serious note, you are instantiating a new Random class every time you want to get a random position that's why you get the same values. Im assuming the random class is based on an internal seed and so you only need one instance and keep using the Next method on a single Random instance."
 
Last edited:
Jakes625

Jakes625

Enthusiast
Messages
58
Reaction score
21
Points
55
Sin$
7
Sorry dude I don't instantiate the Random variable in a loop. I would be a pretty bad programmer if I did haha. I did try and explain this in my post but its hard to explain. The Random is in a class, Which is a food object in my game. I instantiate(spawn) lots of these objects virtually simultaneously at the start of the game and I need to randomize the positions where they spawn but they always return me the same value. (The variable is declared when I instantiate the class)

Would like to know more about other methods of getting Random numbers though, to these ActionScript suggesting these over complicated methods. I don't think these Elliptic Curve Cryptographic methods are really going to be feasible in my game that is designed for mobiles. I just need a simple random integer generator.

P.s Although credit where it is due I think Liquid44 hit the nail on the head with

yeah just initialize random once in the whole project and use next from there on out
 
Top Bottom
Login
Register