zoukankan      html  css  js  c++  java
  • 有规律的随机数

    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?

  • 相关阅读:
    [sql]在case语句中不同情况下then的数据的数据类型不一致ORA-00932: inconsistent datatypes: expected NUMBER got CHAR
    环境迁移 小记
    linux下安装oracle遇到的问题
    正向代理与反向代理
    文件夹与SVN脱离关系
    shell 脚本中$$,$#,$?
    在MySQL中单列索引和联合索引的区别
    Java中Map、HashMap、LinkedHashMap、TreeMap的区别
    Error、Exception与RuntimeException的区别
    设计模式--单例模式
  • 原文地址:https://www.cnblogs.com/lucienbao/p/RegularRandomNumber.html
Copyright © 2011-2022 走看看