C# 抽奖程序winform示例
using System;
using System.Collections.Generic;
using System.Linq;
public class LotterySimulator
{
private Random random = new Random();
public List<string> GenerateWinners(int numberOfWinners, int totalParticipants)
{
List<string> participants = Enumerable.Range(1, totalParticipants)
.Select(i => i.ToString())
.ToList();
List<string> winners = new List<string>();
while (winners.Count < numberOfWinners)
{
int index = random.Next(participants.Count);
string winner = participants[index];
winners.Add(winner);
participants.RemoveAt(index);
}
return winners;
}
}
// 使用示例
class Program
{
static void Main(string[] args)
{
LotterySimulator simulator = new LotterySimulator();
List<string> winners = simulator.GenerateWinners(5, 100); // 假设有100名参与者,抽出5个中奖者
foreach (var winner in winners)
{
Console.WriteLine($"中奖者: {winner}");
}
}
}