编写随机抽号程序可以使用不同的编程语言和方法。以下是几种常见编程语言的随机抽号程序示例:
1. JavaScript 示例
```javascript
function randomDraw(maxNum, count) {
var pool = [];
for (var i = 1; i <= maxNum; i++) {
pool.push(i);
}
var result = [];
while (pool.length > 0) {
var index = Math.floor(Math.random() * pool.length);
var number = pool[index];
result.push(number);
pool.splice(index, 1);
}
return result;
}
// 示例调用
var maxNum = 100;
var count = 10;
var winners = randomDraw(maxNum, count);
console.log(winners);
```
2. Python 示例
```python
import random
def random_draw(names, count):
if count > len(names):
raise ValueError("抽取的号码数量不能大于参与人数")
winners = random.sample(names, count)
return winners
示例调用
participants = ["小明", "小红", "小华", "小张", "小李"]
winner_count = 3
winners = random_draw(participants, winner_count)
print(f"恭喜 {', '.join(winners)} 中奖啦!")
```
3. C 示例
```csharp
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace RandomDrawApp
{
public partial class Form1 : Form
{
private List private Timer timer; public Form1() { InitializeComponent(); timer = new Timer(); timer.Interval = 1000; timer.Tick += Timer_Tick; timer.Start(); } private void Timer_Tick(object sender, EventArgs e) { if (numbers.Count == 50) { timer.Stop(); foreach (var number in numbers) { listBox1.Items.Add(number); } numbers.Clear(); } else { int newNumber = new Random().Next(1, 51); if (!numbers.Contains(newNumber)) { numbers.Add(newNumber); listBox1.Items.Add(newNumber); } } } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "开始") { button1.Text = "停止"; timer.Enabled = true; } else { button1.Text = "开始"; timer.Enabled = false; } } } } ``` 4. Web 示例