C# 随机

WBOY
发布: 2024-09-03 15:15:37
原创
370 人浏览过

用于使用预定义方法生成随机整数的类在 C# 中称为随机类。其中 Next() 方法是 Random 类中最常用的方法,用于生成随机整数,可以以三种形式重载,例如 Next() 方法可以随机返回 -2,147,483,648 和 +2,147,483,648 范围内的整数值, Next(int max) 方法是其他两种形式之一,它可以返回小于指定为 max 的值的整数值,Next(int min, int max) 是另一种形式,返回介于范围之间的整数值指定为最小值和最大值的值。

语法

语法如下:

Random.Next();
Random.Next(int max);
Random.Next(int min, int max);
登录后复制

说明:其中 max 是 Next() 方法必须返回的随机整数值所在的值,min 和 max 指定必须返回的随机整数值的范围一定是撒谎。

C# 随机是如何工作的?

每当需要使用预定义方法生成随机整数时,我们都会使用 C# 中的 Random 类。 next()方法是Random类中最常用的方法,用于生成随机整数,可以通过三种形式重载。 Next() 方法可以随机返回 -2,147,483,648 和 +2,147,483,648 范围内的整数值。 Next(int max) 方法是另外两种形式之一,它可以返回小于指定为 max 的值的整数值。 Next(int min, int max) 是另一种形式,它返回一个整数值,该值位于指定为 min 和 max 的值范围之间。

实现 C# 随机的示例

以下是提到的示例:

示例#1

C# 程序演示 Random 类,利用 Next() 方法生成 -2,147,483,648 和 +2,147,483,648 之间的随机整数:

代码:

using System;
//a class called check is defined
public class check
{
//main method is called within which an instance of the random class is created to be able to make use of Next() method
public static void Main()
{
Random ran = new Random();
//Next() method is called to generate a random integer value between −2,147,483,648 and +2,147,483,648 and stored in an integer variable
int num= ran.Next();
//The random number generated by making use of Next() method of random class is displayed as the output
Console.WriteLine("The Random Number generated by making use of Next() method of random class is: "+num);
}
}
登录后复制

输出:

C# 随机

说明:在上面的程序中,定义了一个名为 check 的类。然后调用 main 方法,在该方法中创建 Random 类的实例,以便能够使用 Next() 方法。然后调用 Next() 方法生成 -2,147,483,648 到 +2,147,483,648 之间的随机整数值,并将其存储在整数变量中。然后,使用随机类的 Next() 方法生成的随机数将显示为输出。输出如上面的快照所示。

示例#2

C# 程序演示 Random 类,利用 Next(int max) 方法生成指定值 max 内的随机整数:

代码:

using System;
//a class called check is defined
public class check
{
//main method is called within which an instance of the Random class is created to be able to make use of Next(int max) method
public static void Main()
{
Random ran = new Random();
// Next(int max) method is called to generate a random integer value which is within the specified value max and stored in an integer variable
int num= ran.Next(50);
//The random number generated by making use of Next(int max) method of random class is displayed as the output
Console.WriteLine("The Random Number within the specified range 50 generated by making use of Next(int max) method of random class is: "+num);
}
}
登录后复制

输出:

C# 随机

说明:在上面的程序中,定义了一个名为 check 的类。然后调用 main 方法,在该方法中创建 Random 类的实例,以便能够使用 Next(int max) 方法。然后调用 Next(int max) 方法生成一个随机整数值,该值在指定为 max 的值范围内,并存储在整数变量中。然后,使用随机类的 Next(int max) 方法生成的随机数将显示为输出。输出如上面的快照所示。

示例 #3

C# 程序演示 Random 类,使用 Next(int min, int max) 方法生成 min 和 max 指定范围内的随机整数:

代码:

using System;
//a class called check is defined
public class check
{
//main method is called within which an instance of the Random class is created to be able to make use of Next(int min, int max) method
public static void Main()
{
Random ran = new Random();
// Next(int min, int Maxx) method is called to generate a random integer value which is within the specified range of values min and max and stored in an integer variable
int num= ran.Next(50,100);
//The random number generated by making use of Next(int min,int max) method of random class is displayed as the output
Console.WriteLine("The Random Number within the specified range between min and max generated by making use of Next(int min, int max) method of a random class is: "+num);
}
}
登录后复制

输出:

C# 随机

说明:在上面的程序中,定义了一个名为 check 的类。然后调用 main 方法,在该方法中创建 Random 类的实例,以便能够使用 Next(int min, int max) 方法。然后调用 Next(int min, int max) 方法生成一个随机整数值,该值在指定的 min 和 max 范围内并存储在整数变量中。然后,使用随机类的 Next(int min, int max) 方法生成的随机数将显示为输出。输出如上面的快照所示。

结论

在本教程中,我们通过编程示例及其输出了解 Random 的定义、语法和工作原理及其方法,了解 C# 中的 Random 概念。

以上是C# 随机的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!