我需要从列表中选择一个满足某些条件的随机元素.我一直在使用的方法有效,但我确信并非一切都有效.最有效的方法是什么?
以下代码位于while(true)循环内,因此在每次迭代时显然不是非常有效地对列表进行洗牌.
Foo randomPick = null;
Collections.shuffle(myList);
for (Foo f : myList) {
if (f.property) {
randomPick = f;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我有一些不寻常的事情需要做.我想知道是否有人能想出一个简单的方法来进行我需要的改变.我拥有的是一个
public class Report
{
public string[] Text { get; set; }
public string[] Image { get; set; }
public string[] Explanation { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
报告类可以包含任意数量的文本,图像和说明,每个数组的大小始终是一致的,但每个报告实例可能不同.
我需要做的是能够以随机顺序对数组元素进行排序.例如,我可能有
Report.Text[0] = "text0";
Report.Text[1] = "text1";
Report.Text[2] = "text2";
Report.Image[0] = "img0";
Report.Image[1] = "img1";
Report.Image[2] = "img2";
Report.Explanation[0] = "exp0";
Report.Explanation[1] = "exp1";
Report.Explanation[2] = "exp2";
Run Code Online (Sandbox Code Playgroud)
然后排序
Report.Text[0] = "text2";
Report.Text[1] = "text0";
Report.Text[2] = "text1";
Report.Image[0] = "img2";
Report.Image[1] = "img0";
Report.Image[2] = "img1";
Report.Explanation[0] = "exp2";
Report.Explanation[1] = …Run Code Online (Sandbox Code Playgroud) 我试图使用Fisher-Yates算法来混淆一堆元素.我无法通过引用传入堆栈.下面的代码给出了错误"Iterators不能有ref或out参数".如何让算法对传入的实际堆栈起作用?
谢谢.
代码如下.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public static class Doshuffle
{
public static IEnumerable<T> Shuffle<T>(ref Stack<T> source)
{
Random rng = new Random();
T[] elements = source.ToArray();
source.Clear();
// Note i > 0 to avoid final pointless iteration
for (int i = elements.Length - 1; i > 0; i--)
{
// Swap element "i" with a random earlier element it (or itself)
int swapIndex = rng.Next(i + 1);
T tmp = elements[i]; …Run Code Online (Sandbox Code Playgroud) 我正在使用此代码生成随机数字序列:
var sequence = Enumerable.Range(0, 9).OrderBy(n => n * n * (new Random()).Next());
Run Code Online (Sandbox Code Playgroud)
一切都很好,直到我需要不止一个序列,在这段代码中我调用例程10次,结果是我的问题,所有序列都是相同的.
int i = 0;
while (i<10)
{
Console.Write("{0}:",i);
var sequence = Enumerable.Range(0, 9).OrderBy(n => n * n * (new Random()).Next());
sequence.ToList().ForEach(x=> Console.Write(x));
i++;
Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)
有人能给我一些实际生成不同序列的提示吗?希望使用LINQ
我已经生成了一个函数来取回一组随机的提交,具体取决于传递给它的数量,但是我担心即使它在大量数据传递时现在只使用少量数据,它也会变得很有效率.引起问题.
有更好的方法来做以下事情吗?
public List<Submission> GetRandomWinners(int id)
{
List<Submission> submissions = new List<Submission>();
int amount = (DbContext().Competitions
.Where(s => s.CompetitionId == id).FirstOrDefault()).NumberWinners;
for (int i = 1 ; i <= amount; i++)
{
bool added = false;
while (!added)
{
bool found = false;
var randSubmissions = DbContext().Submissions
.Where(s => s.CompetitionId == id && s.CorrectAnswer).ToList();
int count = randSubmissions.Count();
int index = new Random().Next(count);
foreach (var sub in submissions)
{
if (sub == randSubmissions.Skip(index).FirstOrDefault())
found = true;
}
if (!found)
{ …Run Code Online (Sandbox Code Playgroud) 我需要帮助来创建一个算法来生成9个随机数.每个号码不得与任何其他号码相同.重写为1-9的9个随机数.
我有这样的想法:
int[] numlist = new int[9];
Random rand = new Random();
int temp;
foreach (int i in numlist) {
temp = rand.Next(1, 10);
//check if temp is already a value of a lower index in numlist
//if so, rolls another random number and checks it again...
numlist[i] = temp;
}
Run Code Online (Sandbox Code Playgroud)
我有一个方法,if内部循环内部循环,而foreach循环内部循环等...