C # and JS random number generation method

Source: Internet
Author: User

The example of C # and JS random number generation is described in this paper. Share to everyone for your reference. Specific as follows:

1. C # generates a random number method:


The code is as follows:

Random rd = new random ();

Rd. Next (Low,high);

Description: Generates a random number of 70-100


The code is as follows:

Random rd = new random ();

Rd. Next (70,100);

2. js Random number method:


The code is as follows:

Math.ceil (Math.random () * (1 + high–low) + low)

Description: Generates a random number of 80-100


The code is as follows:

Math.ceil (Math.random () * (1 + 100–80) + 80)

Method One:


The code is as follows:

function Getrandomnum (Min,max)

{

var Range = max–min;

var Rand = Math.random ();

Return (Min + math.round (Rand * Range));

}

var num = getrandomnum (1,10);

alert (num);

Method Two:


The code is as follows:

var chars = [' 0′, ' 1′, ' 2′, ' 3′, ' 4′, ' 5′, ' 6′, ' 7′, ' 8′, ' 9′, ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z '];

function generatemixed (n) {

var res = "";

for (var i = 0; i < n; i + +) {

var id = math.ceil (math.random () *35);

Res + = Chars[id];

}

return res;

}

Add:

1.math.random (); The result is a random number between 0-1 (including 0, excluding 1)

2.math.floor (num); The parameter num is a numeric value and the function result is the integer portion of Num.

3.math.round (num); The parameter num is a numeric value and the function result is the integer after num is rounded.

4.Math: Mathematical object that provides mathematical calculations of the data.

5.math.random (); Returns a random number between 0 and 1 (including 0, excluding 1).

6.math.ceil (n); Returns the smallest integer greater than or equal to N.

With Math.ceil (Math.random () *10), a random integer of 1 to 10 is obtained, with a minimum probability of 0 being taken.

7.math.round (n); Returns the value of an integer after n rounding.

With Math.Round (Math.random ()), a random integer from 0 to 1 can be obtained evenly.

With Math.Round (Math.random () *10), a basic equalization can be obtained for random integers from 0 to 10, where the probability of obtaining a minimum of 0 and a maximum of 10 is less than half.

8.math.floor (n); Returns the largest integer less than or equal to N.

With Math.floor (Math.random () *10), a random integer from 0 to 9 can be obtained evenly.

I hope this article is helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # and JS random number generation method

This address: http://www.paobuke.com/develop/c-develop/pbk23126.html






Related content protobuf object binary serialized Storage (detailed) Learn to use C # exceptions to talk about the usage and benefits of C # using C # compare two arrays and find methods for the same or different elements
C # implements a simple way to merge Word documents C # Three common vulnerabilities and fixes for method verification codes based on process-oriented weighted average score C # add text to Word documents using OPENXML implementation

C # and JS random number generation method

Related Article

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.