SoFunction
Updated on 2025-03-07

C# and JS random number generation method

This article describes the method of generating random number in C# and js. Share it for your reference. The details are as follows:

1. C# generates random numbers method:

Copy the codeThe code is as follows:
Random rd = new Random();
(low,high);

Description: Generate random numbers of 70-100

Copy the codeThe code is as follows:
Random rd = new Random(); 
(70,100);

2. js random number method:

Copy the codeThe code is as follows:
(() * (1 + high - low) + low)

Description: Generate random numbers of 80-100
Copy the codeThe code is as follows:
(() * (1 + 100 - 80) + 80)

Method 1:

Copy the codeThe code is as follows:
function GetRandomNum(Min,Max)
{
     var Range = Max - Min;
     var Rand = ();
     return(Min + (Rand * Range));
}
var num = GetRandomNum(1,10);
alert(num);

 
Method 2:
Copy the codeThe 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 = (()*35);
         res += chars[id];
     }
     return res;
}

Replenish:

(); The result is a random number between 0-1 (including 0, not 1)
(num); The parameter num is a numeric value, and the result of the function is the integer part of num.
(num); The parameter num is a numeric value, and the result of the function is an integer rounded by num.
: Mathematical object, providing mathematical calculations of data.
(); Returns a random number between 0 and 1 (including 0, not 1).
(n); Returns the smallest integer greater than or equal to n.
When using (()*10);, it mainly obtains random integers from 1 to 10, and the probability of getting 0 is extremely small.
(n); Returns the value of the integer after n rounds.
Use (()); to obtain random integers from 0 to 1 evenly.
When using (()*10);, you can basically obtain random integers from 0 to 10, and the probability of getting the minimum value 0 and maximum value 10 is half the less.
(n); Returns the maximum integer less than or equal to n.
When using (()*10);, random integers from 0 to 9 can be obtained evenly.

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