RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Random Class

Cmonkey123

Wanderer
Random Class

The System.Random class provided in .NET doesn't seem to be all that random...

I have a method that uses this class, it it always produces the same numbers:
Code:
		private void CalculatePop()
		{
			Random rand = new Random();

			//random percentages used to calc long values
			int randPopNotCiv = rand.Next( planetPopPer[0] - 1, planetPopPer[1] + 1 );
			int randPopNotCont = rand.Next( planetPopPer[2] - 1, planetPopPer[3] + 1 );
			int randPopPoor = rand.Next( planetPopPer[4] - 1, planetPopPer[5] + 1 );
			int randPopDiseasednc = rand.Next( planetPopPer[6] - 1, planetPopPer[7] + 1 );
			int randPopDiseasedp = rand.Next( planetPopPer[8] - 1, planetPopPer[9] + 1 );

			planetPopNotCiv = Math.Floor( ( planetPop * randPopNotCiv ) / 100 );
			planetPopNotCont = Math.Floor( ( planetPop * randPopNotCont ) / 100 );
			planetPopPoor = Math.Floor( ( planetPop * randPopPoor ) / 100 );
			planetPopDiseased = Math.Floor( ( ( planetPop * randPopDiseasednc ) / 100 ) + ( ( planetPop * randPopDiseasedp ) / 100 ) );
		}

The planetPopPer values aren't important, I don't have any problems with it... but it seems that no matter how I use this method it produces the same number 99% of the time, unless I completely restart the program.

Am I using the method improperly or is there another, possibly better, method that I could use to generate random numbers?

Any help would be greatly appreciated.
 

arul

Sorceror
The problem is that you create a new instance of the pseudo-random number generator every time you call the method, which is problem for time dependant random number generator.

This should fix your problem
Code:
        public static Random Rand;
	private void CalculatePop()
		{
			if ( Rand == null )
                              Rand = new Random();

			//random percentages used to calc long values
			int randPopNotCiv = Rand.Next( planetPopPer[0] - 1, planetPopPer[1] + 1 );
			int randPopNotCont = Rand.Next( planetPopPer[2] - 1, planetPopPer[3] + 1 );
			int randPopPoor = Rand.Next( planetPopPer[4] - 1, planetPopPer[5] + 1 );
			int randPopDiseasednc = Rand.Next( planetPopPer[6] - 1, planetPopPer[7] + 1 );
			int randPopDiseasedp = Rand.Next( planetPopPer[8] - 1, planetPopPer[9] + 1 );

			planetPopNotCiv = Math.Floor( ( planetPop * randPopNotCiv ) / 100 );
			planetPopNotCont = Math.Floor( ( planetPop * randPopNotCont ) / 100 );
			planetPopPoor = Math.Floor( ( planetPop * randPopPoor ) / 100 );
			planetPopDiseased = Math.Floor( ( ( planetPop * randPopDiseasednc ) / 100 ) + ( ( planetPop * randPopDiseasedp ) / 100 ) );
		}
 

Cmonkey123

Wanderer
Actually, one more question...

What if I am calling a bunch of random operations in the same method? For example I have a method that randomizes an array of 46 integers and then adds them together to get a large double number:

Code:
		private void NewPop()
		{
			if( randNPop == null )
				randNPop = new Random();

			int[] randomPopInt = new int[46];

			int intCount = 0;
			foreach( int i in randomPopInt )
			{
				if( isNotMoon == true )
					randomPopInt[intCount] = randNPop.Next( 21739, 75000000 );
				else
					randomPopInt[intCount] = randNPop.Next( 22, 21739130 );

				planetPop = planetPop + Convert.ToDouble( randomPopInt[intCount].ToString() );

				randomPopInt[intCount] = 0;

				intCount++;
			}
		}

I can't use the NextDouble method either because I need the random number to be between certain values. How would I use the Random class so that their values are more random?
 

arul

Sorceror
Use RandomNumberGenerator class to get "more" random numbers ( from System.Security.Cryptography namespace ).
 

arul

Sorceror
RunUO2874 said:
Or Utility.Random
Utility random uses System.Random to generate random numbers.
Besides that, Cmonkey123 probably wants it for external application.
 

daat99

Moderator
Staff member
arul said:
Utility random uses System.Random to generate random numbers.
Besides that, Cmonkey123 probably wants it for external application.
He can duplicate the utility.random to fit his needs relatively easy :)
 
Top