.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.pswp.cn/news/256182.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/256182.shtml
英文地址,請注明出處:http://en.pswp.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;…