猿问

具有更均匀分布的随机/不可预测数字

我用随机数生成器生成一个随机整数 1-6。我想改变一代以避免这样的情况:

  • 连续第四次生成数字 3

  • 2 号不是在过去 30 代中产生的

所以总的来说,我希望在更短的时间内获得更多水平的数字分布。

我知道这样的数字不再是真正的随机数,但只要它们是不可预测的,这很好。

它看起来像一个常见的问题。有没有典型的解决方案,所以我不会重新发明轮子?

任何语言的代码都可以,但首选 C#。

更新:

我不知道为什么问题会否决票,也许我解释错了..

在评论中,JohnColeman 建议我需要的是随机数生成,因为人类会这样做 - 我认为这是一个非常好的观点。

Fisher Yates Shuffle 也是一个不错的建议。不完美,但在我的情况下有所改进。

我能想到的另一种算法是为每个数字分配一个权重,并使选择这个数字的概率与这个权重成正比。每次选择一个数字时,您可以减少其权重并增加其他数字的权重。但这需要进行测试并且性能可能很差(但这在我的情况下并不重要)。总的来说,我希望这个问题是已知的,并且已经有一些解决方案。


喵喔喔
浏览 162回答 2
2回答

慕姐8265434

好吧,我想我可以将我曾经实施的反向加权(请参阅如何随机均衡不相等的值?)到您的案例。基本上,样本概率与其人口数量成反比。初始人口将是您的指导参数 - 如果它很高,则反向会很低,并且累积计数器几乎没有影响,所以它会非常接近均匀。如果初始人口数较低(例如 1),则累积计数器将更多地影响采样。当您想放弃累积概率并返回原始概率时要考虑的第二个参数,否则低初始计数器的影响会随着时间的推移而消失。代码,使用Math .NET在 [0...6) 范围内进行分类采样,.NET Core 2.2,x64。using System;using System.Linq;using MathNet.Numerics.Random;using MathNet.Numerics.Distributions;namespace EqualizedSampling{    class Program    {        static void Main(string[] args)        {            int increment         = 10; // how much inverse probabilities are updated per sample            int guidanceParameter = 1000000; // Small one - consequtive sampling is more affected by outcome. Large one - closer to uniform sampling            int[]    invprob = new int [6];            double[] probabilities = new double [6];            int[] counter = new int [] {0, 0, 0, 0, 0, 0};            int[] repeat  = new int [] {0, 0, 0, 0, 0, 0};            int prev = -1;            for(int k = 0; k != 100000; ++k ) {                if (k % 60 == 0 ) { // drop accumulation, important for low guidance                    for(int i = 0; i != 6; ++i) {                        invprob[i] = guidanceParameter;                    }                }                for(int i = 0; i != 6; ++i) {                    probabilities[i] = 1.0/(double)invprob[i];                }                var cat = new Categorical(probabilities);                var q = cat.Sample();                counter[q] += 1;                invprob[q] += increment;                if (q == prev)                    repeat[q] += 1;                prev = q;            }            counter.ToList().ForEach(Console.WriteLine);            repeat.ToList().ForEach(Console.WriteLine);        }    }}我计算了重复的对以及数字的总外观。在低引导参数的情况下,连续对的外观更均匀:166701679416713166421659916582243125142489242823672436引导参数为 1000000 时,选择连续对的概率更高166751671216651166771666316622274527072694279226822847更新我们可以添加另一个参数,每个样本递增。较大的增量将使连续采样更不可能。代码更新,输出166591671116618166091675016653218422412285225924252247

小唯快跑啊

我最终修改了 Severin 的解决方案以更好地满足我的需求,所以我想我在这里分享它,以防有人遇到同样的问题。我做了什么:替换Categorical为基于Random类的自己的代码,因为Categorical这给我带来了奇怪的结果。改变了概率的计算方式。添加了更多统计数据。要更改的关键参数是ratio:最小值为 1.0,这使得它的行为就像一个随机数生成器值越高,它就越类似于洗牌算法,因此可以保证数字在不久的将来出现并且不会重复。订单仍然不可预测。比率 1.0 的结果:这就像伪随机数生成一样。3, 5, 3, 3, 3, 3, 0, 3, 3, 5, 5, 5, 2, 1, 3, 5, 3, 3, 2, 3, 1, 0, 4, 1, 5, 1, 3, 5, 1, 5, -Number of occurences:2521218Max occurences in a row:111413Max length where this number did not occur:1413126228比率 5.0 的结果我最喜欢的。很好的分布,偶尔的重复,没有那么长的间隔没有发生一些数字。4, 1, 5, 3, 2, 5, 0, 0, 1, 3, 2, 4, 2, 1, 5, 0, 4, 3, 1, 4, 0, 2, 4, 3, 5, 5, 2, 4, 0, 1, -Number of occurences:555465Max occurences in a row:211112Max length where this number did not occur:71087109比率 1000.0 的结果分布非常均匀,但仍然带有一些随机性。4, 5, 2, 0, 3, 1, 4, 0, 1, 5, 2, 3, 4, 3, 0, 2, 5, 1, 4, 2, 5, 1, 3, 0, 2, 4, 5, 0, 3, 1, -Number of occurences:555555Max occurences in a row:111111Max length where this number did not occur:887867代码:using System;using System.Linq;namespace EqualizedSampling{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static Random rnd = new Random(DateTime.Now.Millisecond);&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// Returns a random int number from [0 .. numNumbers-1] range using probabilities.&nbsp; &nbsp; &nbsp; &nbsp; /// Probabilities have to add up to 1.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; static int Sample(int numNumbers, double[] probabilities)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // probabilities have to add up to 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double r = rnd.NextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double sum = 0.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < numNumbers; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum + probabilities[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sum > r)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return numNumbers - 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const int numNumbers = 6;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const int numSamples = 30;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // low ratio makes everything behave more random&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // min is 1.0 which makes things behave like a random number generator.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // higher ratio makes number selection more "natural"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double ratio = 5.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double[] probabilities = new double[numNumbers];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] counter = new int[numNumbers];&nbsp; &nbsp; &nbsp; &nbsp; // how many times number occured&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] maxRepeat = new int[numNumbers];&nbsp; &nbsp; &nbsp; // how many times in a row this number (max)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] maxDistance = new int[numNumbers];&nbsp; &nbsp; // how many samples happened without this number (max)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] lastOccurence = new int[numNumbers];&nbsp; // last time this number happened&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // init&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < numNumbers; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter[i] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxRepeat[i] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; probabilities[i] = 1.0 / numNumbers;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastOccurence[i] = -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int prev = -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int numRepeats = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int k = 0; k < numSamples; k++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // sample next number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //var cat = new Categorical(probabilities);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //var q = cat.Sample();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var q = Sample(numNumbers, probabilities);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write($"{q}, ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // affect probability of the selected number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; probabilities[q] /= ratio;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // rescale all probabilities so they add up to 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double sumProbabilities = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; probabilities.ToList().ForEach(d => sumProbabilities += d);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < numNumbers; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; probabilities[i] /= sumProbabilities;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // gather statistics&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter[q] += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numRepeats = q == prev ? numRepeats + 1 : 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxRepeat[q] = Math.Max(maxRepeat[q], numRepeats);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastOccurence[q] = k;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < numNumbers; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxDistance[i] = Math.Max(maxDistance[i], k - lastOccurence[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prev = q;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("-\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Number of occurences:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter.ToList().ForEach(Console.WriteLine);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Max occurences in a row:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxRepeat.ToList().ForEach(Console.WriteLine);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Max length where this number did not occur:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxDistance.ToList().ForEach(Console.WriteLine);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答