📅  最后修改于: 2023-12-03 15:13:49.328000             🧑  作者: Mango
Math.Random
is a C# function used to generate random numbers. It generates a random number of type double
between 0 and 1.
double randomNumber = new Random().NextDouble();
To use Math.Random
in your C# code, you need to import the System
namespace:
using System;
Once the namespace is imported, you can use Math.Random
function to generate random numbers.
double randomNumber = new Random().NextDouble();
In the above example, randomNumber
variable will hold a randomly generated double
value between 0 and 1.
By default, Random()
method uses the current system time as the seed value. This ensures that each time the function is called, a different random number is generated. However, if you want predictable behavior of random function in your program, you can set a particular seed value.
Random randObj = new Random(1234); // Setting seed value to 1234
double randomNumber = randObj.NextDouble();
In the above example, Random()
constructor is used to create a new instance of Random
class, with a seed value of 1234
.
C# Math.Random
function is a quick way to generate random numbers in C# language. It is easy to use and provides the flexibility to use custom seed values.