zoukankan      html  css  js  c++  java
  • 已知Random.Next(1,10),产生1至100不重复的随机数据

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace www.cnblogs.com.rock_chen
    {
        class Program
        {
            /// <summary>
            /// 已知Random.Next(1,10),产生1至100不重复的随机数据
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                GenerateRandom generateRandom = new GenerateRandom();
                var result = generateRandom.Calc();
                Console.WriteLine(string.Join(",", result));
                Console.WriteLine();
                Console.WriteLine(string.Join(",", result.OrderBy(p => p).ToArray()));
                Console.Read();
            }
        }
    
        public class GenerateRandom
        {
            Random random = new Random();
    
            public int[] Calc()
            {
                int[] ary = new int[100];
                int count = 0;
                int currentIndex = 0;
                int k = 0;
                while (count < 100)
                {
                    var index = RandomInt();
                    if (ary[currentIndex] == 0)
                    {
                        ary[currentIndex] = index;
                        ary[index - 1] = currentIndex + 1;
                        currentIndex = index - 1;
                        count = 2;
                        continue;
                    }
                    if (ary[index - 1] == 0 && currentIndex != index - 1)
                    {
                        ary[index - 1] = ary[currentIndex];
                        ary[currentIndex] = index;
                        currentIndex = index - 1;
                        count++;
                    }
                    else
                    {
                        while (k < 100 && ary[k] > 0)
                        {
                            k++;
                        }
                        if (currentIndex != k && k < 100)
                        {
                            ary[k] = ary[currentIndex];
                            ary[currentIndex] = k + 1;
                            currentIndex = k;
                            count++;
                        }
                    }
                }
                return ary;
            }
    
            public int RandomInt()
            {
                return (random.Next(1, 10) - 1) * 10//10-80十位数
                    + random.Next(1, 10) + random.Next(1, 10) - 1 //1_17 
                    + (random.Next(1, 10) & 3); //0-3
            }
        }
    }
  • 相关阅读:
    Zabbix的前台SQL注射漏洞利用
    LeetCode OJ--Permutation Sequence *
    LeetCode OJ--Next Permutation *
    LeetCode OJ--Permutations II
    LeetCode OJ--Permutations *
    小算法-计算下一个排列
    LeetCode OJ--Gas Station**
    LeetCode OJ--Insert Interval **
    LeetCode OJ--Search Insert Position
    Ruby自动化测试(操作符的坑)
  • 原文地址:https://www.cnblogs.com/rock_chen/p/2450112.html
Copyright © 2011-2022 走看看