这篇文章主要介绍了c#生成随机数功能,涉及c#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了C#生成随机数功能。分享给大家供大家参考,具体如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace csharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("生成随机数\n");
int randCount = 9;//随机数发的个数
int randMin = 1;//随机数最小值
int randMax = 21;//随机数最大值
int randIndex, flag, temp;
randIndex = temp = flag = 0;
Random rand = new Random();
int[] randArr = new int[randCount];
randArr[0] = rand.Next(randMin, randMax);
while (true)
{
flag = 0;
temp = rand.Next(randMin, randMax);
for (int i = 0; i <= randIndex; i++)
{
if (temp == randArr[i])
{
flag = 1;
break;
}
}
if (flag == 1)//如果 flag == 1 则有重复的数字生成。
{
continue;
}
else if (flag == 0)
{
randIndex++;
randArr[randIndex] = temp;
}
if (randIndex >= randCount - 1)//如果达到 randCount 退出循环
{
break;
}
}
for (int i = 0; i < randCount; i++)
{
Console.WriteLine("arr[" + i + "]=" + randArr[i]);
}
Console.WriteLine("\n任意键退出。");
Console.ReadLine();
}
}
}生成无重复的随机数
运行结果如下:
bee餐饮点餐外卖小程序是针对餐饮行业推出的一套完整的餐饮解决方案,实现了用户在线点餐下单、外卖、叫号排队、支付、配送等功能,完美的使餐饮行业更高效便捷!功能演示:1、桌号管理登录后台,左侧菜单 “桌号管理”,添加并管理你的桌号信息,添加以后在列表你将可以看到 ID 和 密钥,这两个数据用来生成桌子的二维码2、生成桌子二维码例如上面的ID为 308,密钥为 d3PiIY,那么现在去左侧菜单微信设置
1

以上就是详解C#生成随机数功能的代码示例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号