Unity3d C # Generates a true random number

Source: Internet
Author: User

Although it is possible to use the random class to generate stochastic numbers. But it is the system clock seed, so there is a large number of repeated pseudo-random numbers generated.


You can use a RNGCryptoServiceProvider() relative true random number generation.

Generated by the cryptographic service provider (CSP) random number generator (RNG)

Detailed implementations such as the following

    byte[] randombytes = new Byte[4];    RNGCryptoServiceProvider Rngcrypto =    new RNGCryptoServiceProvider ();    Rngcrypto.getbytes (randombytes);    int rngnum = Bitconverter.toint32 (randombytes, 0);//This is a random number

The result is that there are positive negative numbers (seemingly nine and above)


Suppose you want a 0-100-range random number

Rngnum = rngnum% 100;
Take more than you can.


Use the two namespaces that you want to add

Using System.Security.Cryptography;
Using System;



In addition, even though the random number of such methods produces more confusion. But the calculation will result in very large overhead and need attention.


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Unity3d C # Generates a true random number

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.