最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中Random.Next方法的使用小結(jié)

 更新時(shí)間:2024年01月22日 10:14:52   作者:wenchm  
在C#中,Random.Next()方法用于生成一個(gè)隨機(jī)整數(shù),本文主要介紹了C#中Random.Next方法的使用小結(jié),具有一定的參考價(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)文章

最新評(píng)論

莲花县| 祁门县| 济源市| 古交市| 綦江县| 会理县| 个旧市| 巨鹿县| 辽阳市| 集安市| 五常市| 信阳市| 巩义市| 平度市| 高州市| 齐河县| 泰来县| 壶关县| 塘沽区| 彭阳县| 平利县| 广河县| 邢台市| 墨脱县| 皋兰县| 囊谦县| 巴南区| 金山区| 芒康县| 栾川县| 盱眙县| 荃湾区| 砚山县| 文登市| 双鸭山市| 长汀县| 公主岭市| 鄯善县| 蓬莱市| 峨山| 昭平县|