Auxiliary class -- random function (randomhelper) auxiliary class

Source: Internet
Author: User
Tags float max

 

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.