Random Number Generation

View previous topic View next topic Go down

Random Number Generation

Post by Fred U. Derf on Tue Jul 14, 2009 1:43 am

This is a very long post, sorry.

Back on around the 26th of June 2009, Karel_II wrote:

Raed wrote:With the _090626_1 version,
the more girls you have in a building, the slower it seems to get. A
full central palace seems to take at least a second to load.



This bug is caused, timers for refreshing Random gennerators... Sad Code was fast to much.. And generators gives same numbers... I must add some slowdown to code...

I will try to find beter way to solve this problem...



and I respond that adding slowdown code is not the best way to solve this problem.

The current (well last version that I looked at it in detail was the May 09 version but the 090709_1 source base seems to still have the problem) is an improper use of the C# Random class. The Random class will supply a fresh random value as the return value of its Next method.

The values, of course, are not mathematically random values, but rather are pseudo-random numbers which are computed from an initial seed value. Over the years mathematicians have developed several equations for generating pseudo-random sequences of values e.g. sequences that have the statistical characteristics very similar to the statistical characteristics of a sequence of true random values. Google pseudo-random number generator for more info.

So anyway, the C# Random class constructor has 2 flavors. One in which the seed for the generator is explicitly supplied. And another, the default, takes no parameters and derives the seed value from the computer's clock.

The current code base mis-uses the Random class by constructing a new instance of the class (using the default constructor) every time the code wants a fresh random value. e.g. This idiom for getting a fresh random value is used throughout the code:

Random rnd = new Random();
int rndnum = rnd.Next(......some expression....);

so every time we want a random value we create a fresh generator using the computer clock as the seed. and since the code runs fairly fast (good!) the computer clock has not advanced between several of our calls to the constructor. and so we get identical values from the generator because each instance of the generator (most likely) has the same seed (the slow computer clock).

The solution is to have 1 and only 1 instance of the Random class. Get rid of all the Random rnd = new Random(); statements. Have the single random number generator as a global value. any code that requires a fresh random value references that global random generator.

Use the Singleton Design Pattern for the sole random number generator in the program.

Google: singleton pattern in csharp




Sorry for the overly lengthy message. Hope this helps. Feel free to ask me questions about this if any of the above information is not clear to you.
------
Fred

Fred U. Derf

Posts : 6
Reputation : 0
Join date : 2009-07-14

View user profile

Back to top Go down

Re: Random Number Generation

Post by Morax on Wed Jul 15, 2009 8:08 pm

hm, my reply has been deleted, so here it is again:

put the line

public static Random random = new Random;

into GameEngine then you can access it from anywhere through GameEngine.random, i.e.

GameEngine.rand.Next();

it's not singleton but something similar that's less complicated.

Morax

Posts : 5
Reputation : 0
Join date : 2009-07-08

View user profile

Back to top Go down

Re: Random Number Generation

Post by Karel_II on Thu Jul 16, 2009 6:47 pm

to Morax: I read your old post and use your improvement... it has been release in next version...

Thanks

_________________
Sory for my bad english Sad

Have a nice day

Karel_II

My neznalí vedeni nevedomými, děláme nemožné pro nevděčné..
avatar
Karel_II

Posts : 192
Reputation : 31
Join date : 2009-06-19
Age : 34
Location : Czech Republic

View user profile

Back to top Go down

Re: Random Number Generation

Post by steved on Fri Oct 23, 2009 10:48 am

Not sure if you are still looking for a solution to this, but it seems possible that you are as the game slows down on me and my copy seemed to have been released yesterday.

I am not sure if this is a possibility, but a lot of time the game does little to nothing; can you have it fill an array with random numbers when it is just sitting and pull from that array when random numbers are required?

steved

Posts : 9
Reputation : 0
Join date : 2009-10-23

View user profile

Back to top Go down

Re: Random Number Generation

Post by Karel_II on Fri Oct 23, 2009 1:05 pm

Game slow down is not caused random generators...

DataTables is caused this problem, which remain from old version.. I try remove this bug...

_________________
Sory for my bad english Sad

Have a nice day

Karel_II

My neznalí vedeni nevedomými, děláme nemožné pro nevděčné..
avatar
Karel_II

Posts : 192
Reputation : 31
Join date : 2009-06-19
Age : 34
Location : Czech Republic

View user profile

Back to top Go down

Re: Random Number Generation

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum