c and js random number generation methods

  • 2020-12-20 03:44:08
  • OfStack

This article describes the c# and js random number generation methods. Share to everybody for everybody reference. The details are as follows:

1. C# random number generation method:

Random rd = new Random();
rd.Next(low,high);

Generate 70-100 random numbers

Random rd = new Random();  
rd.Next(70,100);

2. js random number method:

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

Generate 80-100 random numbers
Math.ceil(Math.random() * (1 + 100 - 80) + 80)

Method 1:

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 2:
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;
}

Supplement:

1.Math.random(); The result is 1 random number between 0 and 1 (including 0, excluding 1).
2.Math.floor(num); The parameter num is 1 value, and the result of the function is the integer part of num.
3.Math.round(num); The parameter num is 1 value, and the function result is the integer after rounding num4 into 5.
4.Math: Mathematical object that provides mathematical calculation of data.
5.Math.random(); Returns 1 random number between 0 and 1 (including 0, not including 1).
6.Math.ceil(n); Returns the smallest integer greater than or equal to n.
Use Math. ceil (Math random (10); , mainly getting random integers from 1 to 10, with a very small chance of getting 0.
7.Math.round(n); Returns the value of an integer rounded to n4.
Use Math. round (Math random ()); Random integers from 0 to 1 can be equalized.
Use Math. round (Math random (10); , the random integer from 0 to 10 can be obtained by basic equilibrium, and the probability of obtaining the minimum value of 0 and the maximum value of 10 is less than 1 half.
8.Math.floor(n); Returns the largest integer less than or equal to n.
Use Math. floor (Math random (10); , a random integer from 0 to 9 can be equalized.

Hopefully this article has helped you with your C# programming.


Related articles: