如何用C或C++编写一个程序,随机从10个数中选出3个。

如何用C或C++编写一个程序,随机从10个数中选出3个。,第1张

这样应该是随机的。

#include <stdioh>

#include <stdlibh>

#include <timeh>

#define N 10

#define M 3

void random(int a, int n, int b, int m)

{

int i, j, t;

for (i = 0; i < n; i++)

{

j = rand() % n;

t = a[i];

a[i] = a[j];

a[j] = t;

}

for (i = 0; i < m; i++)

b[i] = a[i];

}

void main()

{

int i;

int a[N] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, b[M];

srand(time(NULL));

do {

random(a, N, b, M);

for (i = 0; i < M; i++)

printf("%d ", b[i]);

printf("\n");

} while(getchar() != EOF);

}

双色球随机选:

Private Sub Command1_Click()

Dim a(6) As Integer

Dim b As Integer

For i = 1 To 6

a(i) = Int(Rnd() (1 + (33 - 1))) + 1

For j = 1 To i - 1

If a(i) = a(j) Then

i = i - 1

End If

Next

Next

For i = 1 To 6

For j = i + 1 To 6

If a(i) < a(j) Then

T = a(i): a(i) = a(j): a(j) = T

End If

Next

Next

b = Int(Rnd() (1 + (16 - 1))) + 1

Text1Text = a(6) & " " & a(5) & " " & a(4) _

& " " & a(3) & " " & a(2) & " " & a(1) & " +" & b

End Sub

List 可以通过数组的形式、以元素下标获得具体位置的值;

那你就对 listsize() 也就是list 的长度, 但这里需要注意,长度假如说是 5 的话,那么你只能生成 0~4 的随机数,要不然在提取值的时候会发生数组越界的异常。

一个整型数值的随机会的吧?每次提取就随机一下;如果想每次随机得到的值跟上一个不一样,那就多加个 if 判断。

路过~希望可以帮助你

Private Sub Command1_Click()

Randomize

Label1Caption = List1List(CInt(Rnd (List1ListCount - 1)))

End Sub

Private Sub Form_Load()

Dim str As String

Dim str1() As String

str = InputBox("请输入数字,以''隔开", "输入数字")

str1 = Split(str, "")

For i = 0 To UBound(str1)

List1AddItem str1(i)

Next

Randomize

Label1Caption = List1List(CInt(Rnd (List1ListCount - 1)))

End Sub

建立两个Command1和2

Dim RedBalls, BullBalls

Private Sub Command1_Click()

'Label1Caption = ""

rarr = GetRndNotRepeat(0, UBound(RedBalls), 6)

barr = GetRndNotRepeat(0, UBound(BullBalls), 1)

For i = 1 To 6

t = Now

Do

DoEvents

Loop While DateDiff("s", t, Now) < 15

FillStyle = 0

FillColor = vbRed

Form1Circle (500 + i 500, 1000), 200, vbRed

CurrentX = 350 + i 500

CurrentY = 840

Print Format(RedBalls(rarr(i)), "00")

Next i

t = Now

Do

DoEvents

Loop While DateDiff("s", t, Now) < 2

i = 7

FillStyle = 0

FillColor = vbBlue

Form1Circle (500 + i 500, 1000), 200, vbBlue

CurrentX = 350 + i 500

CurrentY = 840

Print Format(BullBalls(barr(1)), "00")

End Sub

Private Sub Command2_Click()

End

End Sub

Private Sub Form_Load()

RedBalls = Array(2, 3, 4, 7, 8, 9, 12, 13, 14, 17, 18, 19, 22, 23, 24, 27, 28, 29, 32, 33)

BullBalls = Array(3, 4, 5, 9, 13, 14, 15)

'Label1Caption = ""

Command1Caption = "开始"

Command2Caption = "退出"

Form1FontSize = 15

Form1FontName = "Arial"

Form1ForeColor = &HFFFFFF

End Sub

Public Function GetRndNotRepeat(ByVal NumMin As Integer, ByVal NumMax As Integer, ByVal n As Integer)

Dim arr() As Integer

If n > NumMax - NumMin + 1 Then

ReDim arr(0)

arr(0) = 0

Else

ReDim arr(n)

Dim b() As Byte

Dim m As Integer

m = Int((NumMax - NumMin) / 8)

ReDim b(m)

Dim x As Integer, y As Integer

Dim z As Byte

Randomize

arr(0) = 1

For i = 1 To n

Do

x = Int(Rnd (NumMax - NumMin + 1)) + NumMin

y = x - NumMin

z = 2 ^ (y Mod 8)

y = y \ 8

Loop While b(y) And z

b(y) = b(y) Or z

arr(i) = x

Next i

End If

GetRndNotRepeat = arr

End Function

已经运行过。

以上就是关于如何用C或C++编写一个程序,随机从10个数中选出3个。全部的内容,包括:如何用C或C++编写一个程序,随机从10个数中选出3个。、如何用VB编写一个简单的选号器就是点击然后程序随机帮选出一个数(1~70个数)、求一个从list容器中随机选择的程序 谢谢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9750136.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存