• 有规律的随机数


    using System;
    using System.Collections;

    namespace RandomDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                Hashtable ht0 = new Hashtable();
                for (int i = 0; i < 6; i++)
                {
                    do
                    {
                        Random rnd = new Random((unchecked((int)(DateTime.Now.Ticks))));
                        int tmp = rnd.Next(0, 100);
                        if (!ht0.ContainsValue(tmp))
                        {
                            ht0.Add(i, tmp);
                            break;
                        }
                    }
                    while (true);
                }
                Console.WriteLine("===============");
                foreach (var item in ht0.Values)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine("===============");


                Hashtable ht1 = new Hashtable();
                for (int i = 0; i < 6; i++)
                {
                    do
                    {
                        Random rnd = new Random(Guid.NewGuid().GetHashCode());
                        int tmp = rnd.Next(0, 100);
                        if (!ht1.ContainsValue(tmp))
                        {
                            ht1.Add(i, tmp);
                            break;
                        }
                    }
                    while (true);
                }
                Console.WriteLine("===============");
                foreach (var item in ht1.Values)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine("===============");

                Console.ReadKey();
            }


        }
    }

    结果1:

    ===============
    65
    90
    15
    41
    66
    91
    ===============
    ===============
    91
    97
    92
    30
    90
    12
    ===============

    结果2:

    ===============
    25
    72
    19
    66
    13
    60
    ===============
    ===============
    70
    26
    48
    16
    77
    86
    ===============

    结果3:

    ===============
    76
    51
    26
    75
    50
    24
    ===============
    ===============
    39
    33
    28
    83
    53
    50
    ===============

    结果4:

    ===============
    13
    88
    63
    38
    12
    87
    ===============
    ===============
    87
    92
    73
    6
    88
    11
    ===============

    结果5:

    ===============
    29
    4
    78
    53
    28
    2
    ===============
    ===============
    64
    65
    46
    35
    94
    53
    ===============

    结果6:

    ===============
    32
    57
    83
    8
    33
    59
    ===============
    ===============
    86
    54
    38
    84
    37
    58
    ===============

    So! How to get random numbers?

  • 相关阅读:
    使用sourcetree 出现 For user git on host github.com 解决方案
    iOS App Store下载的付费软件 重新签名后 安装到未越狱的手机上
    Swift 入门之简单语法(一)
    [转]C语言指针 之 结构体指针
    Xcode 菜单详解 [转载自用]
    C语言笔记-字符串+转义符(写起来想砸键盘)
    C语言笔记-getchar等蛋疼设定 [可能已解决]
    C语言笔记-枚举类型
    C语言笔记-数据类型长度
    C语言笔记-函数-存储期和作用域
  • 原文地址:https://www.cnblogs.com/lucienbao/p/RegularRandomNumber.html
Copyright © 2020-2023  润新知