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?

  • 相关阅读:
    从Swift3的标准库协议看面向协议编程(一)
    iOS多线程到底不安全在哪里?
    typealias和泛型接口
    iOS蓝牙开发CoreBluetooth快速入门
    iOS学习路线图
    iOS开发masonry的一些使用简介
    在Swift项目中使用cocoaPods导入第三方OC库
    微信小程序开发POST请求
    一些NSArray,NSDictionary,NSSet相关的算法知识
    iOS开发中的权限
  • 原文地址:https://www.cnblogs.com/lucienbao/p/RegularRandomNumber.html
Copyright © 2011-2022 走看看