C# 范围内的非重复整数(不包括0)生成重复整数序列
参考,我想创建一个非重复的随机整数生成器,但它的值介于2到9之间(包括2到9) 我充分利用了上面url中答案的包装代码C# 范围内的非重复整数(不包括0)生成重复整数序列,c#,loops,random,integer,C#,Loops,Random,Integer,参考,我想创建一个非重复的随机整数生成器,但它的值介于2到9之间(包括2到9) 我充分利用了上面url中答案的包装代码 int oldrand = <prior random number>; int adder = randomNumberGenerator() % 4; int newrand = (oldrand + adder + 1) % 5; 正如你所见,5和9像一个模式一样重复,而在另一个序列中,你可以看到所有四个9 代码的问题在于,您需要为所需的每个新随机数选择一个
int oldrand = <prior random number>;
int adder = randomNumberGenerator() % 4;
int newrand = (oldrand + adder + 1) % 5;
正如你所见,5和9像一个模式一样重复,而在另一个序列中,你可以看到所有四个9 代码的问题在于,您需要为所需的每个新随机数选择一个新的随机数。您的代码反复重复使用单个随机数,因此您只需获得一个数学上可预测的序列,并且根据您从何处开始,您将获得重复的序列 下面是一个有效的代码版本(并测试结果…它的编写方式我认为更具可读性、可重用性和可理解性):
static void Main(字符串[]args)
{
随机=新随机();
常数int max=8;
对于(int i=0;i<50;i++)
{
int[]序列=
GetRandomNonConsecutiveSequence(随机,最大,4)
.选择(j=>j+2)
.ToArray();
如果(!检查序列(序列))
{
Console.WriteLine(“序列失败:+string.Join(“,”,序列));
}
}
}
私有静态布尔校验序列(int[]序列)
{
int previous=序列[0];
for(int i=1;i0)
{
收益率返回上一次=下一次(上一次、最大、随机);
}
}
静态int-Next(int-previous、int-max、Random-Random)
{
返回(上一个+随机。下一个(最大值-1)+1)%max;
}
您的代码的问题是,您需要为所需的每个新随机数选择一个新的随机数。您的代码反复重复使用单个随机数,因此您只需获得一个数学上可预测的序列,并且根据您从何处开始,您将获得重复的序列
下面是一个有效的代码版本(并测试结果…它的编写方式我认为更具可读性、可重用性和可理解性):
static void Main(字符串[]args)
{
随机=新随机();
常数int max=8;
对于(int i=0;i<50;i++)
{
int[]序列=
GetRandomNonConsecutiveSequence(随机,最大,4)
.选择(j=>j+2)
.ToArray();
如果(!检查序列(序列))
{
Console.WriteLine(“序列失败:+string.Join(“,”,序列));
}
}
}
私有静态布尔校验序列(int[]序列)
{
int previous=序列[0];
for(int i=1;i0)
{
收益率返回上一次=下一次(上一次、最大、随机);
}
}
静态int-Next(int-previous、int-max、Random-Random)
{
返回(上一个+随机。下一个(最大值-1)+1)%max;
}
不确定为什么要对每个循环执行4个newrand
s-但如果确实要为每个循环创建4个样本,则应该在每个循环之间重新生成加法器
-否则,您没有正确地遵循该模式。它多久生成一次此结果?不确定为什么要对每个循环执行4个newrand
s-但是如果确实要为每个循环创建4个样本,则应该在每个循环之间重新生成加法器
-否则,你没有正确地遵循这个模式。它多久产生一次这样的结果?我还没有完全理解IEnumerable部分,因为我还在自学。你能详细说明一下密码吗?谢谢我还没有完全理解IEnumerable部分,因为我还在自学。你能详细说明一下密码吗?谢谢
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
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;
}