improve:点名算法

改进不放回随机
This commit is contained in:
2025-12-20 19:57:11 +08:00
parent d011d2ba8a
commit 7f01e7acb6
2 changed files with 96 additions and 67 deletions
+44 -19
View File
@@ -212,59 +212,84 @@ namespace Ink_Canvas
Random random = new Random();// randSeed + DateTime.Now.Millisecond / 10 % 10);
string outputString = "";
List<string> outputs = new List<string>();
List<int> rands = new List<int>();
LabelOutput2.Visibility = Visibility.Collapsed;
LabelOutput3.Visibility = Visibility.Collapsed;
new Thread(() =>
{
var animationPool = new List<int>();
for (int num = 1; num <= PeopleCount; num++)
{
animationPool.Add(num);
}
int lastDisplayedIndex = -1;
for (int i = 0; i < RandWaitingTimes; i++)
{
int rand = random.Next(1, PeopleCount + 1);
while (rands.Contains(rand))
if (animationPool.Count == 0)
{
rand = random.Next(1, PeopleCount + 1);
animationPool.Clear();
for (int num = 1; num <= PeopleCount; num++)
{
animationPool.Add(num);
}
}
rands.Add(rand);
if (rands.Count >= PeopleCount) rands = new List<int>();
int randomIndex = random.Next(0, animationPool.Count);
int selectedNumber = animationPool[randomIndex];
int lastIndex = animationPool.Count - 1;
if (randomIndex != lastIndex)
{
animationPool[randomIndex] = animationPool[lastIndex];
}
animationPool.RemoveAt(lastIndex);
Application.Current.Dispatcher.Invoke(() =>
{
if (Names.Count != 0)
{
LabelOutput.Content = Names[rand - 1];
LabelOutput.Content = Names[selectedNumber - 1];
}
else
{
LabelOutput.Content = rand.ToString();
LabelOutput.Content = selectedNumber.ToString();
}
});
Thread.Sleep(RandWaitingThreadSleepTime);
}
rands = new List<int>();
Application.Current.Dispatcher.Invoke(() =>
{
for (int i = 0; i < TotalCount; i++)
var candidatePool = new List<int>();
for (int num = 1; num <= PeopleCount; num++)
{
int rand = random.Next(1, PeopleCount + 1);
while (rands.Contains(rand))
candidatePool.Add(num);
}
for (int i = 0; i < TotalCount && candidatePool.Count > 0; i++)
{
int randomIndex = random.Next(0, candidatePool.Count);
int selectedNumber = candidatePool[randomIndex];
int lastIndex = candidatePool.Count - 1;
if (randomIndex != lastIndex)
{
rand = random.Next(1, PeopleCount + 1);
candidatePool[randomIndex] = candidatePool[lastIndex];
}
rands.Add(rand);
if (rands.Count >= PeopleCount) rands = new List<int>();
candidatePool.RemoveAt(lastIndex);
if (Names.Count != 0)
{
outputs.Add(Names[rand - 1]);
outputString += Names[rand - 1] + Environment.NewLine;
outputs.Add(Names[selectedNumber - 1]);
outputString += Names[selectedNumber - 1] + Environment.NewLine;
}
else
{
outputs.Add(rand.ToString());
outputString += rand + Environment.NewLine;
outputs.Add(selectedNumber.ToString());
outputString += selectedNumber + Environment.NewLine;
}
}
if (TotalCount <= 5)