C# 无重复项的随机值
不重复地生成随机值的最佳方法是什么?顺便说一句:它是WPF,而不是控制台。不清楚你所说的重复是什么意思。如果您不想在一行中看到相同的数字,那么只需保留一个哈希表或C# 无重复项的随机值,c#,random,int,duplicates,C#,Random,Int,Duplicates,不重复地生成随机值的最佳方法是什么?顺便说一句:它是WPF,而不是控制台。不清楚你所说的重复是什么意思。如果您不想在一行中看到相同的数字,那么只需保留一个哈希表或字典,其中保留最后一个整数。然后检查下一个数字是否与上一个数字相同。如果不是,请从字典中删除最后一个数字,并放入当前数字。如果它们相同,则从random请求另一个随机整数 Random r = new Random(); int randomvalue = r.Next(1,20); * * if(randomvalue == 1) s
字典
,其中保留最后一个整数。然后检查下一个数字是否与上一个数字相同。如果不是,请从字典中删除最后一个数字,并放入当前数字。如果它们相同,则从random
请求另一个随机整数
Random r = new Random();
int randomvalue = r.Next(1,20);
*
*
if(randomvalue == 1) something
if(randomvalue == 2) something
*
*
if(randomvalue == 19) something
var myDictionary=新字典;
var randomvalue=r.Next(1,20);
while(myDictionary.ContainsKey(randomvalue))
{
随机值=r.Next(1,20);
}
myDictionary.Clear();
myDictionary.Add(随机值,123)//123只是一个数字。没关系。
这保证了两个相同的整数永远不会连续出现
注意::其他答案很有创意,但“了解您的数据结构”。不要使用列表进行查找。散列是我们使用的。请尝试以下方法: Random randomInstance=新建Random()
List NumList=new List();
AddRange(新的int[]{1,2,3,4,5,6,7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30 });
int index=randomInstance.Next(0,NumList.Count-1);
int randomNumber=NumList[index];
NumList.RemoveAt(索引);
存储随机值是使其唯一的唯一方法
试试这个:
List<int> NumList = new List<int>();
NumList.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30 });
int index = randomInstance.Next(0, NumList.Count - 1);
int randomNumber = NumList[index];
NumList.RemoveAt(index);
List<int> i = new List<int>(new int[] { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20});
List<int> r;
r = ShuffleList(i);
}
private static List<E> ShuffleList<E>(List<E> unshuffledList)
{
List<E> sList = new List<E>();
Random r = new Random();
int randomIndex = 0;
while (unshuffledList.Count > 0)
{
randomIndex = r.Next(0, unshuffledList.Count);
sList.Add(unshuffledList[randomIndex]);
unshuffledList.RemoveAt(randomIndex); //remove so wont be added a second time
}
return sList; //return the new shuffled list
}
}
Random r=new Random();
public List randomList=新列表();
int随机值=0;
公共编号()
{
随机值=r.Next(0,20);
如果(!randomList.Contains(randomvalue))
{
随机列表。添加(随机值);
如果(随机值==1)
//做点什么
如果(随机值==N)
//做点什么
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
命名空间杂技
{
公共静态类启动器
{
公共静态void Main()
{
//1至20个,无重复项
List ns=新列表();
随机r=新随机();
Int32 ph=0;
而(ns.计数<20)
{
而(ns.Contains(ph=r.Next(1,21)){
ns.添加(ph);
}
//ns现在使用随机唯一数字(1到20)填充
}
}
}
//这样就行了
班级计划
{
静态void Main(字符串[]参数)
{
using System;
using System.Collections;
using System.Collections.Generic;
namespace SOFAcrobatics
{
public static class Launcher
{
public static void Main ()
{
// 1 to 20 without duplicates
List<Int32> ns = new List<Int32>();
Random r = new Random();
Int32 ph = 0;
while (ns.Count < 20)
{
while (ns.Contains (ph = r.Next(1, 21))) {}
ns.Add(ph);
}
// ns is now populated with random unique numbers (1 to 20)
}
}
}
List i=新列表(新int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20});
清单r;
r=随机列表(i);
}
私有静态列表ShuffleList(列表取消缓冲列表)
{
List sList=新列表();
随机r=新随机();
int随机指数=0;
而(unsuffledlist.Count>0)
{
randomIndex=r.Next(0,unsuffledlist.Count);
添加(unsuffledlist[randomIndex]);
unsuffledlist.RemoveAt(randomIndex);//删除,因此不会再次添加
}
return sList;//返回新的无序列表
}
}
试试这个:
List<int> NumList = new List<int>();
NumList.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30 });
int index = randomInstance.Next(0, NumList.Count - 1);
int randomNumber = NumList[index];
NumList.RemoveAt(index);
List<int> i = new List<int>(new int[] { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20});
List<int> r;
r = ShuffleList(i);
}
private static List<E> ShuffleList<E>(List<E> unshuffledList)
{
List<E> sList = new List<E>();
Random r = new Random();
int randomIndex = 0;
while (unshuffledList.Count > 0)
{
randomIndex = r.Next(0, unshuffledList.Count);
sList.Add(unshuffledList[randomIndex]);
unshuffledList.RemoveAt(randomIndex); //remove so wont be added a second time
}
return sList; //return the new shuffled list
}
}
创造你的范围20和洗牌。然后可以按顺序使用随机值。你的意思是连续重复还是从不使用同一个数字两次?可能重复的,你能给我举个例子吗?我能问你,我应该在哪里以及如何创建我的字典吗?@PetrKonecny我把它添加到我的答案中。快速填充列表的方法(需要System.Linq)。。。NumList.AddRange(Enumerable.Range(startValue,endValue);如果期望的结果是每个数字只使用一次(例如,以随机顺序问一堆测验问题)然后我认为这是最好的解决方案,因为它只需要n个循环。使用其他方法跟踪已经使用的随机数将导致许多循环无所事事,只是试图找到一个未使用的随机数。虽然不太可能,但循环可能永远不会结束。准确地标记。这是一种方法,您的列表可以n有一个连续数字列表或任何element@Maddy-如果调用
.Next(0,NumList.Count-1)
,则永远不会得到列表中的最后一项。调用.Next(…)的第二个参数
是唯一的上限。没有人,它仍然不起作用。我不知道为什么,你的代码看起来应该起作用。我不知道。
var rnd = new Random();
var shuffled =
Enumerable
.Range(1, 20)
.OrderBy(x => rnd.Next())
.ToArray();