Auxiliary class of Random Functions (randomhelper)
Take a look at the randomhelper class in one of the helper classes. It is not frequently used in a single project, but almost any game uses random number generation, making the game content appear less cyclical and bringing more changes to the game.
In the breakout game, you will write some random numbers. Here the bricks (blocks) are randomly generated. The random probability of level 1 is 10%, the random rate of level 2 is 20%, and so on. In this way, as the level increases, the game becomes more difficult. You can only use the random class to call the next method to generate a new random number, but if you want to generate a random standardization vector, you have to write the followingCodeRow:
Random randomgenerator = new random (INT) datetime. now. ticks); vector3 randomnormalvector = new vector3 (float) randomgenerator. nextdouble () * 2.0f-1.0f, (float) randomgenerator. nextdouble () * 2.0f-1.0f, (float) randomgenerator. nextdouble () * 2.0f-1.0f); randomnormalvector. normalize ();
Replace this code repeatedly, such as randomhelper's helper class will be very useful. Figure 3-8 shows the basic structure of the randomhelper class.
Figure 3-8
As you can see, these methods are simple, and the entire class can be written in just a few minutes. However, this class is very useful, thanks to the internal globalrandomgenerator instance of the random class. The randomhelper class generates a random number much faster than creating a new random class every time you need a random number.
Generate random vectors
Here you can see a method of the randomhelper class:
/// <Summary> /// get random vector2 /// </Summary> /// <Param name = "min"> minimum for each component </param> // /<Param name = "Max"> maximum for each component </param> // <returns> vector2 </returns> Public static vector2 getrandomvector2 (float min, float max) {return New vector2 (getrandomfloat (Min, max), getrandomfloat (Min, max);} // getrandomvector2 (Min, max)
It doesn't make much sense to test any method in the randomhelper class, because all returned values are random numbers. You don't have to check whether the getrandomvector2 method returns a vector2; it returns vector2. Basically, no error occurs.