C#中Random.Next方法的使用小結(jié)
一、重載
返回一個(gè)隨機(jī)整數(shù)。
Next() | 返回一個(gè)非負(fù)隨機(jī)整數(shù)。 |
Next(Int32) | 返回一個(gè)小于所指定最大值的非負(fù)隨機(jī)整數(shù)。 |
Next(Int32, Int32) | 返回在指定范圍內(nèi)的任意整數(shù)。 |
二、Next()
返回一個(gè)非負(fù)隨機(jī)整數(shù)。
1.定義
public virtual int Next (); 返回 Int32 大于或等于 0 且小于 Int32.MaxValue 的 32 位有符號(hào)整數(shù)。
2.示例
//Next() namespace ConsoleApp39 { internal class Program { private static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); Random rnd = new(); Console.WriteLine("Generating 10 random numbers:"); for (uint i = 1; i <= 10; i++) Console.WriteLine($"{rnd.Next(),15:N0}"); } } } // 運(yùn)行結(jié)果: /* Generating 10 random numbers: 1,346,240,124 723,453,914 1,735,589,449 1,860,966,112 1,562,174,739 22,804,240 576,594,210 1,204,251,909 1,436,476,117 828,029,130 */
Random.Next 生成一個(gè)隨機(jī)數(shù),其值范圍為 0 到小于 Int32.MaxValue。 若要生成值范圍為 0 到某個(gè)其他正數(shù)的隨機(jī)數(shù),請(qǐng)使用 Random.Next(Int32) 方法重載。 若要在不同的范圍內(nèi)生成隨機(jī)數(shù),請(qǐng)使用 Random.Next(Int32, Int32) 方法重載。
三、Next(Int32)
返回一個(gè)小于所指定最大值的非負(fù)隨機(jī)整數(shù)。
1.定義
public virtual int Next (int maxValue); 參數(shù) maxValue Int32 要生成的隨機(jī)數(shù)的上限(隨機(jī)數(shù)不能取該上限值)。 maxValue 必須大于或等于 0。 返回 Int32 大于或等于零且小于 maxValue 的 32 位有符號(hào)整數(shù),即:返回值的范圍通常包括零但不包括 maxValue。 但是,如果 maxValue 等于 0,則返回 0。 例外 ArgumentOutOfRangeException maxValue 小于 0。
2.示例1
// Next(Int32) namespace ConsoleApp40 { internal class Program { private static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); Console.WriteLine( "This example of the Random.Next() methods\n" + "generates the following output.\n"); Console.WriteLine( "Create Random objects all with the same seed and " + "generate\nsequences of numbers with different " + "bounds. Note the effect\nthat the various " + "combinations of bounds have on the sequences."); NoBoundsRandoms(234); UpperBoundRandoms(234, int.MaxValue); UpperBoundRandoms(234, 2000000000); UpperBoundRandoms(234, 200000000); BothBoundsRandoms(234, 0, int.MaxValue); BothBoundsRandoms(234, int.MinValue, int.MaxValue); BothBoundsRandoms(234, -2000000000, 2000000000); BothBoundsRandoms(234, -200000000, 200000000); BothBoundsRandoms(234, -2000, 2000); // Generate random numbers with no bounds specified. void NoBoundsRandoms(int seed) { Console.WriteLine( "\nRandom object, seed = {0}, no bounds:", seed); Random randObj = new(seed); // Generate six random integers from 0 to int.MaxValue. for (int j = 0; j < 6; j++) Console.Write("{0,11} ", randObj.Next()); Console.WriteLine(); } // Generate random numbers with an upper bound specified. void UpperBoundRandoms(int seed, int upper) { Console.WriteLine( "\nRandom object, seed = {0}, upper bound = {1}:", seed, upper); Random randObj = new(seed); // Generate six random integers from 0 to the upper bound. for (int j = 0; j < 6; j++) Console.Write("{0,11} ", randObj.Next(upper)); Console.WriteLine(); } // Generate random numbers with both bounds specified. void BothBoundsRandoms(int seed, int lower, int upper) { Console.WriteLine( "\nRandom object, seed = {0}, lower = {1}, " + "upper = {2}:", seed, lower, upper); Random randObj = new(seed); // Generate six random integers from the lower to // upper bounds. for (int j = 0; j < 6; j++) Console.Write("{0,11} ", randObj.Next(lower, upper)); Console.WriteLine(); } } } } /* This example of the Random.Next() methods generates the following output. Create Random objects all with the same seed and generate sequences of numbers with different bounds. Note the effect that the various combinations of bounds have on the sequences. Random object, seed = 234, no bounds: 2091148258 1024955023 711273344 1081917183 1833298756 109460588 Random object, seed = 234, upper bound = 2147483647: 2091148258 1024955023 711273344 1081917183 1833298756 109460588 Random object, seed = 234, upper bound = 2000000000: 1947533580 954563751 662424922 1007613896 1707392518 101943116 Random object, seed = 234, upper bound = 200000000: 194753358 95456375 66242492 100761389 170739251 10194311 Random object, seed = 234, lower = 0, upper = 2147483647: 2091148258 1024955023 711273344 1081917183 1833298756 109460588 Random object, seed = 234, lower = -2147483648, upper = 2147483647: 2034812868 -97573602 -724936960 16350718 1519113864 -1928562472 Random object, seed = 234, lower = -2000000000, upper = 2000000000: 1895067160 -90872498 -675150156 15227793 1414785036 -1796113767 Random object, seed = 234, lower = -200000000, upper = 200000000: 189506716 -9087250 -67515016 1522779 141478503 -179611377 Random object, seed = 234, lower = -2000, upper = 2000: 1895 -91 -676 15 1414 -1797 */
3.示例2
生成一個(gè)隨機(jī)整數(shù),該整數(shù)用作索引從數(shù)組中檢索字符串值。 由于數(shù)組的最高索引小于其長(zhǎng)度的 1,因此 屬性的值 Array.Length 作為 maxValue 參數(shù)提供。
// Next(Int32) namespace ConsoleApp41 { internal class Program { private static void Main(string[] args) { Random rnd = new(); string[] malePetNames = [ "Rufus", "Bear", "Dakota", "Fido", "Vanya", "Samuel", "Koani", "Volodya", "Prince", "Yiska" ]; string[] femalePetNames = [ "Maggie", "Penny", "Saya", "Princess", "Abby", "Laila", "Sadie", "Olivia", "Starlight", "Talla" ]; // Generate random indexes for pet names. int mIndex = rnd.Next(malePetNames.Length); int fIndex = rnd.Next(femalePetNames.Length); // Display the result. Console.WriteLine("Suggested pet name of the day: "); Console.WriteLine(" For a male: {0}", malePetNames[mIndex]); Console.WriteLine(" For a female: {0}", femalePetNames[fIndex]); } } } // 運(yùn)行結(jié)果: /* Suggested pet name of the day: For a male: Samuel For a female: Abby */
四、Next(Int32, Int32)
返回在指定范圍內(nèi)的任意整數(shù)。
1.定義
public virtual int Next (int minValue, int maxValue); 參數(shù) minValue Int32 返回的隨機(jī)數(shù)的下界(隨機(jī)數(shù)可取該下界值)。 maxValue Int32 返回的隨機(jī)數(shù)的上界(隨機(jī)數(shù)不能取該上界值)。 maxValue 必須大于或等于 minValue。 返回 Int32 一個(gè)大于等于 minValue 且小于 maxValue 的 32 位帶符號(hào)整數(shù),即:返回的值范圍包括 minValue 但不包括 maxValue。 如果 minValue 等于 maxValue,則返回 minValue。 例外 ArgumentOutOfRangeException minValue 大于 maxValue。
重載 Next(Int32, Int32) 返回范圍從 minValue 到 maxValue - 1 的隨機(jī)整數(shù)。 但是,如果 maxValue 等于 minValue,則 方法返回 minValue。與僅返回非負(fù)值的方法的其他重載 Next 不同,此方法可以返回負(fù)隨機(jī)整數(shù)。
2.示例1
使用 Random.Next(Int32, Int32) 方法生成具有三個(gè)不同范圍的隨機(jī)整數(shù)。 請(qǐng)注意,此示例的確切輸出取決于傳遞給 Random 類構(gòu)造函數(shù)的系統(tǒng)提供的種子值。
// Next(Int32, Int32) namespace ConsoleApp42 { internal class Program { private static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); Random rnd = new(); Console.WriteLine("\n20 random integers from -100 to 100:"); for (int ctr = 1; ctr <= 20; ctr++) { Console.Write("{0,6}", rnd.Next(-100, 101)); if (ctr % 5 == 0) Console.WriteLine(); } Console.WriteLine("\n20 random integers from 1000 to 10000:"); for (int ctr = 1; ctr <= 20; ctr++) { Console.Write("{0,8}", rnd.Next(1000, 10001)); if (ctr % 5 == 0) Console.WriteLine(); } Console.WriteLine("\n20 random integers from 1 to 10:"); for (int ctr = 1; ctr <= 20; ctr++) { Console.Write("{0,6}", rnd.Next(1, 11)); if (ctr % 5 == 0) Console.WriteLine(); } } } } // 運(yùn)行結(jié)果: /* 20 random integers from -100 to 100: -78 -13 58 -11 80 -49 -5 -57 81 -44 69 28 -63 -23 65 96 -20 -37 76 18 20 random integers from 1000 to 10000: 3738 6382 4790 2334 1875 6557 6491 2583 2012 6601 5929 4639 7814 3045 1737 5512 4275 8866 2288 7201 20 random integers from 1 to 10: 4 7 1 5 7 2 7 6 3 4 10 6 3 3 7 1 3 1 10 6 */
3.示例2
使用 Random.Next(Int32, Int32) 方法生成具有三個(gè)不同范圍的隨機(jī)整數(shù)。 請(qǐng)注意,此示例的確切輸出取決于傳遞給 Random 類構(gòu)造函數(shù)的系統(tǒng)提供的種子值。
// Next(Int32, Int32) namespace ConsoleApp43 { internal class Program { private static void Main(string[] args) { Random rnd = new(); string[] malePetNames = [ "Rufus", "Bear", "Dakota", "Fido", "Vanya", "Samuel", "Koani", "Volodya", "Prince", "Yiska" ]; string[] femalePetNames = [ "Maggie", "Penny", "Saya", "Princess", "Abby", "Laila", "Sadie", "Olivia", "Starlight", "Talla" ]; // Generate random indexes for pet names. int mIndex = rnd.Next(0, malePetNames.Length); int fIndex = rnd.Next(0, femalePetNames.Length); // Display the result. Console.WriteLine("Suggested pet name of the day: "); Console.WriteLine(" For a male: {0}", malePetNames[mIndex]); Console.WriteLine(" For a female: {0}", femalePetNames[fIndex]); } } } // 運(yùn)行結(jié)果: /* Suggested pet name of the day: For a male: Samuel For a female: Penny */
到此這篇關(guān)于C#中String.PadRight方法的使用小結(jié)的文章就介紹到這了,更多相關(guān)C# String.PadRight內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C#實(shí)現(xiàn)順序表(線性表)完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)順序表(線性表)的方法,結(jié)合完整實(shí)例形式分析了順序表的原理及C#相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-06-06C#版的 Escape() 和 Unescape() 函數(shù)分享
從網(wǎng)上看到兩個(gè)方法, C# 版的 Escape() 和 Unescape(),收藏下。2011-05-05WinForm中實(shí)現(xiàn)picturebox自適應(yīng)圖片大小的方法
這篇文章主要介紹了WinForm中實(shí)現(xiàn)picturebox自適應(yīng)圖片大小的方法,涉及pictureBox控件相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下2017-05-05C#實(shí)現(xiàn)HTTP訪問(wèn)類HttpHelper的示例詳解
在項(xiàng)目開發(fā)過(guò)程中,我們經(jīng)常會(huì)訪問(wèn)第三方接口,如我們需要接入的第三方接口是Web API,這時(shí)候我們就需要使用HttpHelper調(diào)用遠(yuǎn)程接口了。本文為大家介紹了C#實(shí)現(xiàn)HTTP訪問(wèn)類HttpHelper的示例代碼,需要的可以參考一下2022-09-09C#實(shí)現(xiàn)軟件開機(jī)自啟動(dòng)功能(不需要管理員權(quán)限)
在本文中,我們探討了如何使用C#語(yǔ)言實(shí)現(xiàn)應(yīng)用程序在系統(tǒng)啟動(dòng)時(shí)自動(dòng)運(yùn)行的功能,同時(shí)避免了對(duì)管理員權(quán)限的需求,文章通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2025-04-04