1到35随机给我选5个不重复数字
时间: 2024-03-25 15:36:32 浏览: 463
好的,我可以帮您生成这样的5个不重复的数字。这里给您一个示例代码:
```python
import random
nums = random.sample(range(1, 36), 5)
print(nums)
```
这个代码会在 1 到 35 的范围内随机生成 5 个不重复的数字,并输出到屏幕上。您可以多运行几次代码来验证生成的数字是否都是不同的。
相关问题
EXCEL VBA从1到35的数字当众随机挑选7个且不重复,1到12至少出现一个,13-24至少出现1个,25到35至少出现一个中的数字,将结果放在SHEET2的A列。从1-12中随机挑选3个数字且不重复,将结果放在SHEET2的B列
Sub RandomNumbers()
Dim arrNumbers(1 To 35) As Integer
Dim arrResult(1 To 7) As Integer
Dim arrBonus(1 To 3) As Integer
Dim i As Integer, j As Integer, k As Integer, n As Integer
Dim temp As Integer, bonusCount As Integer
'将1到35的数字存入数组
For i = 1 To 35
arrNumbers(i) = i
Next i
'将数组中的数字随机排序
For i = 1 To 35
j = Int(Rnd() * 35) + 1
temp = arrNumbers(i)
arrNumbers(i) = arrNumbers(j)
arrNumbers(j) = temp
Next i
'从1到12中至少选一个数字
arrResult(1) = Int(Rnd() * 12) + 1
'从13到24中至少选一个数字
arrResult(2) = Int(Rnd() * 12) + 13
'从25到35中至少选一个数字
arrResult(3) = Int(Rnd() * 11) + 25
'随机选取4到7个数字
For i = 4 To 7
Do
n = Int(Rnd() * 35) + 1
Loop Until Not IsInArray(arrResult, n, i - 1)
arrResult(i) = n
Next i
'将结果存入Sheet2的A列
For i = 1 To 7
Cells(i, 1).Value = arrResult(i)
Next i
'从1到12中随机选取3个数字
For i = 1 To 3
Do
n = Int(Rnd() * 12) + 1
Loop Until Not IsInArray(arrBonus, n, i - 1)
arrBonus(i) = n
Next i
'将结果存入Sheet2的B列
For i = 1 To 3
Cells(i, 2).Value = arrBonus(i)
Next i
End Sub
Function IsInArray(arr As Variant, val As Variant, endIndex As Integer) As Boolean
Dim i As Integer
For i = 1 To endIndex
If arr(i) = val Then
IsInArray = True
Exit Function
End If
Next i
IsInArray = False
End Function
用java写一个生成彩票号码的小程序,[1,33]之间(包含1和33)随机选七个不重复的数字,并把生成的彩票号码打印在控制台上。
好的,以下是用Java编写的生成彩票号码的小程序:
```java
import java.util.Random;
public class LotteryGenerator {
public static void main(String[] args) {
Random random = new Random();
int[] lotteryNumbers = new int[7];
for (int i = 0; i < 7; i++) {
int num;
do {
num = random.nextInt(33) + 1;
} while (contains(lotteryNumbers, num));
lotteryNumbers[i] = num;
}
System.out.println("彩票号码为:");
for (int num : lotteryNumbers) {
System.out.print(num + " ");
}
}
private static boolean contains(int[] arr, int num) {
for (int i : arr) {
if (i == num) {
return true;
}
}
return false;
}
}
```
程序会生成七个不重复的随机数字,然后将它们打印在控制台上。每个数字都在1到33之间。
阅读全文