.net生成随机字符串

生成随机字符串的工具类:

    /// <summary>/// 随机字符串工具类/// </summary>public class RandomTools{/// <summary>/// 随机系数/// </summary>public static int _RandIndex = 0;#region 获取某个区间的一个随机数/// <summary>/// 获取某个区间的一个随机数/// </summary>/// <param name="minimum">开始区间</param>/// <param name="maximum">结束区间</param>/// <param name="length">小数点的位数</param>/// <param name="isSleep">是否线程睡眠</param>/// <param name="millisecondsTimeout">线程时间</param>/// <returns>返回某个区间的一个随机数</returns>public double GetRandomNumber(double minimum, double maximum, int length, bool isSleep = false, int millisecondsTimeout = 5){if (isSleep){System.Threading.Thread.Sleep(millisecondsTimeout);}Random random = new Random();return Math.Round((random.NextDouble() * (maximum - minimum) + minimum), length);}#endregion#region 生成数字随机数【随机数大小有区间限制】/// <summary>/// 数字随机数/// </summary>/// <param name="minNum">随机数的最小值</param>/// <param name="maxNum">随机数的最大值</param>/// <returns>从多少到多少之间的数据 包括开始不包括结束</returns>public static int RndInt(int minNum, int maxNum){if (_RandIndex >= 1000000) _RandIndex = 1;Random rnd = new Random(DateTime.Now.Millisecond + _RandIndex);_RandIndex++;return rnd.Next(minNum, maxNum);}public static IList<int> RndInt(int num1, int num2, int len){IList<int> list = new List<int>();for (int i = 0; i < len; i++) list.Add(RndInt(num1, num2));return list;}public static IList<int> RndInt(int len){IList<int> list = RndInt(0, int.MaxValue, len);return list;}#endregion#region 生成数字随机数【随机数有长度的限制】/// <summary>/// 数字随机数/// </summary>/// <param name="length">生成长度</param>/// <returns>返回指定长度的数字随机串</returns>public static string RndNum(int length){if (_RandIndex >= 1000000) _RandIndex = 1;char[] arrChar = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };StringBuilder num = new StringBuilder();Random rnd = new Random(DateTime.Now.Millisecond + _RandIndex);for (int i = 0; i < length; i++){num.Append(arrChar[rnd.Next(0, 9)].ToString());}return num.ToString();}#endregion#region 生成日期随机字符串/// <summary>/// 日期随机函数/// </summary>/// <returns>返回日期随机串</returns>public static string RndDateStr(){return DateTime.Now.ToString("yyyyMMddHHmmssfff") + RandomTools.RndInt(1000, 9999).ToString();}public static IList<string> RndDateStr(int len){IList<string> list = new List<string>();for (int i = 0; i < len; i++) list.Add(RndDateStr());return list;}#endregion#region 生成数字和字母的随机字符串/// <summary>/// 数字和字母随机数/// </summary>/// <param name="length">生成长度</param>/// <returns>返回指定长度的数字和字母的随机串</returns>public static string RndCode(int length){if (_RandIndex >= 1000000) _RandIndex = 1;char[] arrChar = new char[]{'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'};System.Text.StringBuilder num = new System.Text.StringBuilder();Random rnd = new Random(DateTime.Now.Millisecond + _RandIndex);for (int i = 0; i < length; i++){num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());}return num.ToString();}public static IList<string> RndCodeList(int len){IList<string> list = new List<string>();for (int i = 0; i < len; i++) list.Add(RndCode(len));return list;}#endregion#region 生成字母的随机字符串/// <summary>/// 字母随机数/// </summary>/// <param name="length">生成长度</param>/// <returns>返回指定长度的字母随机数</returns>public static string RndLetter(int length){if (_RandIndex >= 1000000) _RandIndex = 1;char[] arrChar = new char[]{'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x','_','A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'};StringBuilder num = new StringBuilder();Random rnd = new Random(DateTime.Now.Millisecond + _RandIndex);for (int i = 0; i < length; i++){num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());}return num.ToString();}public static IList<string> RndLetterList(int len){IList<string> list = new List<string>();for (int i = 0; i < len; i++) list.Add(RndLetter(len));return list;}#endregion#region GetGuid/// <summary>/// 生成GUID/// </summary>/// <returns></returns>public static string GetGuid(){System.Guid g = System.Guid.NewGuid();return g.ToString();}public static IList<string> GetGuid(int len){IList<string> list = new List<string>();for (int i = 0; i < len; i++) list.Add(GetGuid());return list;}#endregion}

 

转载于:https://www.cnblogs.com/linJie1930906722/p/5968168.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/256182.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【图像处理】——Python鼠标框选ROI(感兴趣)区域并且保存(含鼠标事件)

鼠标交互切割矩形 接下来,就是本文重点了。先吐个槽,网上有资源,但搜到的都是C++的。本来有点气馁的,还好,有官网在,文档写得很清楚,而且接口函数名字变化不大,稍微做下修改就行了。 import cv2global img global point1, point2 def on_mouse(event, x, y, flags, pa…

c++ 11 override final

C 11添加了两个继承控制关键字&#xff1a;override和final。 override确保在派生类中声明的重载函数跟基类的虚函数有相同的签名。final阻止类的进一步派生和虚函数的进一步重载 出处&#xff1a;http://www.cnblogs.com/zhangdongsheng/ 作者&#xff1a;张东升

泛型方法与桥方法

Java泛型中有存在一种方式叫做类型擦除&#xff0c;也就是说泛型在编译期间进行类型检验上做到有效安全&#xff0c;但是在运行当中&#xff0c;会将该泛型类型用顶层父类&#xff08;若无继承关系则用Object&#xff09;代替&#xff0c;然后再进行强转换成目标类型&#xff0…

Pytorch基础(九)——损失函数

一、概念 损失函数在深度学习领域是用来计算搭建模型预测的输出值和真实值之间的误差。 具体实现过程&#xff1a;在一个批次&#xff08;batch&#xff09;前向传播完成后&#xff0c;得到预测值&#xff0c;然后损失函数计算出预测值和真实值之间的差值&#xff0c;反向传播…

用程序猿思维、程序设计师思维两种方式写求斐波那契数列的方法。

//用Java实现斐波那契数列(Fibonacci) public class Test {public int f(int n)//n代表第几个数字。程序返回它相应的值{return n>2?f(n-1)f(n-2):1;//看似如此优雅的一句程序}//程序设计师的思维&#xff1a;会重构上面的代码。让他们更易读。推荐&#xff01;&#xff01…

【图像处理】——图像的差集、并集、补集、交集以及两个图像相减出现负数的处理方法

目录 目录 1、交集 2、差集 3、并集 4、补集 5、差为负值,和超过255的解决办法

Pytorch基础(十)——优化器(SGD,Adagrad,RMSprop,Adam,LBFGS等)

一、概念 Pytorch中优化器的目的&#xff1a;将损失函数计算出的差值Loss减小。 优化过程&#xff1a;优化器计算网络参数的梯度&#xff0c;然后使用一定的算法策略来对参数进行计算&#xff0c;用新的参数来重新进行训练&#xff0c;最终降低Loss。 其中官网提供了13种优化算…

【图像处理】——改变图像的大小(降采样重采样)下采样和上采样

转载自:https://jingyan.baidu.com/article/a3a3f81139be1f8da2eb8ade.html 上采样、下采样和金字塔加速参考:https://blog.csdn.net/Eastmount/article/details/89341077 目录 1、拉伸图片——重采样 2、缩小图片 1)三次插值法cv2.INTER_CUBIC

一段代码到可执行程序所有经历

如果你写的代码是hello.c&#xff0c;你的程序将经历下面的步骤到达硬盘或者内存成为可执行文件。 第一步&#xff1a;hello.c&#xff08;文本&#xff09;经过预编译生成hello.i&#xff08;文本&#xff09; 第二步&#xff1a;hello.i&#xff08;文本&#xff09;经过编译…

js获取url参数值

今天碰到要在一个页面获取另外一个页面url传过来的参数&#xff0c;一开始很本能的想到了用 split("?")这样一步步的分解出需要的参数。 后来想了一下&#xff0c;肯定会有更加简单的方法的&#xff01;所以在网上找到了两个很又简单实用的方法&#xff0c;mark下 方…

[PyCharm]unindent does not match any outer indentation level解决方法

转载&#xff1a;https://www.jianshu.com/p/b34f30717eb2 问题出现原因 1、代码前后缩进量不一致 2、tab和space混用&#xff08;如果一段代码既使用space又使用tab进行缩进&#xff0c;会发生错误&#xff0c;这个时候PyCharm会自动进行判断&#xff0c;根据设置的预先缩进…

为什么要选择Apache Pulsar(二)

这是介绍Apache Pulsar关键特性系列文章的第二篇。Pulsar是由Yahoo开发并开源的下一代发布订阅消息系统。在第一篇文章里&#xff0c;我们介绍了Pulsar对消息模型的灵活支持、多租户、多地域复制和持久性。在这一篇文章里&#xff0c;我们将继续介绍Pulsar的IO隔离机制、伸缩性…

Yolov5目标检测模型运行遇到的相关问题汇总

一、yolov5-5.0常见错误 1. pycocotools工具包无法安装 具体报错如下&#xff1a; requirements: pycocotools>2.0 not found and is required by YOLOv5 pkg_resources.DistributionNotFound: The pycocotools>2.0 distribution was not found and is required by th…

PHP反射之类的反射

最近在琢磨如何用PHP实现站点的插件功能&#xff0c;需要用到反射&#xff0c;于是现学了一下&#xff0c;笔记如下&#xff1a; class Person {public $name Lily;public $gender male;public $age 20;public function eat(){echo Lily is eating!;}public function run(){…

数据结构(复习)--------关于平衡二叉树(转载)

在上一个专题中&#xff0c;我们在谈论二叉查找树的效率的时候。不同结构的二叉查找树&#xff0c;查找效率有很大的不同&#xff08;单支树结构的查找效率退化成了顺序查找&#xff09;。如何解决这个问题呢&#xff1f;关键在于如何最大限度的减小树的深度。正是基于这个想法…

mysql外键

效果 a,b,c 如果c设置到a的外键&#xff0c;那么只能在删除c的记录后&#xff0c;才能删除a的记录。 https://stackoverflow.com/questions/1905470/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails CREATE TABLE IF NOT EXISTS advertisers ( adverti…

C++总结笔记(一)—— 基础知识汇总

很长时间没有再复习C的基础知识&#xff0c;现在将一些容易遗忘的知识点做一个简单的汇总。 1、注释 ❤️分为单行注释和多行注释 //cout<<endl;/*int i1;cout<<i<<endl;*/2、常量 ❤️宏常量&#xff1a;#define &#xff0c;宏常量没有类型&#xff0c;…

微软自带iscsi客户端对iqn的要求

节点名称&#xff1a;Microsoft iSCSI 发起程序严格遵守为 iSCSI 节点名称指定的规则。这些规则也适用于 Microsoft iSCSI 发起程序节点名称以及发现的任何目标节点名称。构建 iSCSI 节点名称的规则&#xff08;如 iSCSI 规范以及“iSCSI 名称的字符串配置文件”Internet 草稿中…

【Python数据结构】——链表

仅仅为了记录 # 定义一个类&#xff0c;用于创建链表的结点 class LNode():def __init__(self,elem,next_ None):# 类的初始化方法,在实例化类的时候会自动调用self.elem elemself.next next_list1 LNode(1)# 类的实例化&#xff0c;LNode(1)为第一个链表结点&#xff0c;…