范围内的非重复整数(排除0)产生重复的整数序列

时间:2017-03-16 07:15:58

标签: c# loops random integer

参考generating random numbers without consecutive repetition,我想制作一个非重复的随机整数生成器,但介于2到9之间。

我很好地利用了上面网址中答案的包装代码。

int oldrand = <prior random number>;
int adder = randomNumberGenerator() % 4;
int newrand = (oldrand + adder + 1) % 5;

我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Random Rand = new Random();
            int oldrand = Rand.Next(0,7);
            //Console.WriteLine(oldrand);
            for (int i=0; i<50;i++) {

            int adder = Rand.Next(0,7) % 7;
            int newrand = (oldrand + adder + 1) % 8+2;
            int newrand2 = (newrand + adder + 1) % 8+2;
            int newrand3 = (newrand2 + adder + 1) % 8+2;
            int newrand4 = (newrand3 + adder + 1) % 8+2;

            Console.WriteLine(newrand);
            Console.WriteLine(newrand2);
            Console.WriteLine(newrand3);
            Console.WriteLine(newrand4);
            oldrand = newrand4;
            Console.WriteLine();
            }
        }
    }

    public class Rand {
        private static readonly Random globalRandom = new Random(); 
        private static readonly object globalLock = new object();

        private static readonly ThreadLocal<Random> threadRandom = new ThreadLocal<Random>(NewRandom);

        public static Random NewRandom() 
        { 
            lock (globalLock) 
            { 
                return new Random(globalRandom.Next()); 
            } 
        }

        public static Random Instance { get { return threadRandom.Value; } }

        public static int Next(int minValue, int maxValue) 
        { 
            return Instance.Next(minValue, maxValue); 
        }

    }
}

产生一个奇怪的结果:

2
7
4
9

2
3
4
5

2
7
4
9

5
9
5
9

8
7
6
5

3
9
7
5

2
7
4
9

5
9
5
9

5
9
5
9

9
9
9
9

5
9
5
9

7
5
3
9

8
7
6
5

3
9
7
5

9
5
9
5

4
3
2
9

6
3
8
5

2
7
4
9

6
3
8
5

9
5
9
5

正如你所看到的,5和9重复像一个模式,而在另一个序列中你看到所有四个9。

1 个答案:

答案 0 :(得分:0)

您的代码存在的问题是您需要为所需的每个新随机数选择一个新的随机数。您的代码会反复使用单个随机数,因此您只需获得数学上可预测的序列,并且根据您的起点,您将获得重复项。

这是一个有效的代码版本(并测试结果......它也以我认为更具可读性,可重用性和可理解性的方式编写):

    static void Main(string[] args)
    {
        Random random = new Random();
        const int max = 8;

        for (int i = 0; i < 50; i++)
        {
            int[] sequence =
                GetRandomNonConsecutiveSequence(random, max, 4)
                .Select(j => j + 2)
                .ToArray();

            if (!CheckSequence(sequence))
            {
                Console.WriteLine("Sequence failed: " + string.Join(", ", sequence));
            }
        }
    }

    private static bool CheckSequence(int[] sequence)
    {
        int previous = sequence[0];

        for (int i = 1; i < sequence.Length; i++)
        {
            if (previous == sequence[i])
            {
                return false;
            }

            previous = sequence[i];
        }

        return true;
    }

    private static IEnumerable<int> GetRandomNonConsecutiveSequence(
        Random random, int max, int count)
    {
        int previous = random.Next(max);

        yield return previous;

        while (--count > 0)
        {
            yield return previous = Next(previous, max, random);
        }
    }

    static int Next(int previous, int max, Random random)
    {
        return (previous + random.Next(max - 1) + 1) % max;
    }
相关问题