注册 登录
编程论坛 VB6论坛

有80个随机数,怎样只给固定位置的数排列?

hxx1021 发布于 2023-08-19 16:11, 1998 次点击
有80个随机数,怎样只给固定位置的数排列?
例如只给1-10的随机数排列,给11-20、21-30、31-40、41-50、51-60、61-70、71-80的随机数排列,示例见图片,源码见附件。
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
程序代码:
Option Explicit

Option Base 1

Private Declare Function timeGetTime Lib "winmm.dll" () As Long

Dim a(1 To 7) As Long

Const Nums = 80  '定义一个常量80

Private Sub Command1_Click()

Dim i, j, n, t As Long

Dim KLB(Nums) As Long  '定义一个变量

Dim s, m As String       '定义字符串

t = timeGetTime

Randomize

For i = 1 To Nums      'i = 1 到 80

    n = Int(Rnd * Nums) + 1   'n为随机数

     If KLB(n) = 0 Then

        KLB(n) = n
  
      If n < 10 Then '判断是否是小于10
  
          n = 0 & n '小于10则在前面加0
      End If

s = s & n & " "    's = s & n & vbCrLf


Else

   i = i - 1

End If

Next i

    Text1.Text = s        't = timeGetTime - t      'MsgBox t


End Sub

Private Sub Command2_Click()

  Dim b(10) As Integer  '定义一个一维数组

Dim i, j As Integer

Dim m As String


For i = 1 To 10
   b(i) = Int(Rnd * 90 + 10)  '装入一批随机数
   Print b(i);  '打印输出,后面一个分号就不换行
Next i

For i = 1 To 9
    For j = i + 1 To 10
        If b(i) > b(j) Then  '第一个和第二个比较
           m = b(i)
           b(i) = b(j)
           b(j) = m
        End If
Next j, i


Print
For i = 1 To 10
    Print b(i);  '打印输出,后面一个分号就不换行
Next i
End Sub



Private Sub Command3_Click()
    Const Nums = 80
    Dim KLB(Nums) As Long
    Dim i, j, l, h As Long
   
    Dim s, t, n As String       '定义字符串
   
   
   
    Randomize
   
    For i = 1 To Nums      'i = 1 到 80
        n = Int(Rnd * Nums) + 1  'n为随机数
        KLB(i) = n
    Next i
   
    s = ""
    For i = 1 To Nums
        s = s & KLB(i) & " "
    Next i
   
    'Debug.Print s
     
    l = 10
    h = 30
    For i = l To h - 1
        For j = i To h
            If KLB(i) > KLB(j) Then
                t = KLB(i)
                KLB(i) = KLB(j)
                KLB(j) = t
            End If
        Next j
    Next i
     

   
    s = ""
    For i = 1 To Nums
        s = s & KLB(i) & " "
    Next i
   ' Debug.Print s
    Text1.Text = s
   
End Sub

Private Sub Form_DblClick()
    Const Nums = 80
    Dim KLB(Nums) As Long
    Dim i, j, n, l, h As Long
   
    Dim s, t As String       '定义字符串
   
   
   
    Randomize
   
    For i = 1 To Nums      'i = 1 到 80
        n = Int(Rnd * Nums) + 1  'n为随机数
        KLB(i) = n
    Next i
   
    s = ""
    For i = 1 To Nums
        s = s & KLB(i) & " "
    Next i
    Debug.Print s
     
    l = 10
    h = 30
    For i = l To h - 1
        For j = i To h
            If KLB(i) > KLB(j) Then
                t = KLB(i)
                KLB(i) = KLB(j)
                KLB(j) = t
            End If
        Next j
    Next i
     

   
    s = ""
    For i = 1 To Nums
        s = s & KLB(i) & " "
    Next i
    Debug.Print s
   
End Sub
9 回复
#2
hxx10212023-08-19 16:13
目前只有随机数,求教进行排序的方法,最好能在源码上修改,谢谢!
#3
apull2023-08-20 11:40
你把它定义为8行10列的二维数组不是更方便?
按你的修改了下
程序代码:

Private Sub Command1_Click()

    Dim i, j, n, t As Long

    Dim KLB(Nums) As Long  '定义一个变量,标注已生成
    Dim NUM(Nums) As Long  '''实际顺序

    Dim s As String, m As String       '定义字符串

    t = timeGetTime

    Randomize

    For i = 1 To Nums      'i = 1 到 80
        n = Int(Rnd * Nums) + 1   'n为随机数
        If KLB(n) = 0 Then
            KLB(n) = n
            NUM(i) = n
            'If n < 10 Then '判断是否是小于10      
            '  n = 0 & n '小于10则在前面加0
            'End If

        's = s & n & " "    's = s & n & vbCrLf

        Else
           i = i - 1
        End If
        
    Next i
    '''''''
    Dim inx, k, tmp
    For i = 1 To 8     '注数
        inx = (i - 1) * 10 + 1     '每行起始索引
        For j = inx To inx + 8   
            For k = j + 1 To inx + 9
                If NUM(j) > NUM(k) Then
                        tmp = NUM(j)
                        NUM(j) = NUM(k)
                        NUM(k) = tmp
                End If
            Next k
        Next j
    Next i
   
    For i = 1 To Nums
            s = s & Format(NUM(i), "0#") & " "
    Next i
    ''''''
    Text1.Text = s        't = timeGetTime - t      'MsgBox t
End Sub



[此贴子已经被作者于2023-8-20 12:57编辑过]

#4
hxx10212023-08-20 13:18
以下是引用apull在2023-8-20 11:40:16的发言:

你把它定义为8行10列的二维数组不是更方便?
按你的修改了下

Private Sub Command1_Click()

    Dim i, j, n, t As Long

    Dim KLB(Nums) As Long  '定义一个变量,标注已生成
    Dim NUM(Nums) As Long  '''实际顺序

    Dim s As String, m As String       '定义字符串

    t = timeGetTime

    Randomize

    For i = 1 To Nums      'i = 1 到 80
        n = Int(Rnd * Nums) + 1   'n为随机数
        If KLB(n) = 0 Then
            KLB(n) = n
            NUM(i) = n
            'If n < 10 Then '判断是否是小于10      
            '  n = 0 & n '小于10则在前面加0
            'End If

        's = s & n & " "    's = s & n & vbCrLf

        Else
           i = i - 1
        End If
        
    Next i
    '''''''
    Dim inx, k, tmp
    For i = 1 To 8     '注数
        inx = (i - 1) * 10 + 1     '每行起始索引
        For j = inx To inx + 8   
            For k = j + 1 To inx + 9
                If NUM(j) > NUM(k) Then
                        tmp = NUM(j)
                        NUM(j) = NUM(k)
                        NUM(k) = tmp
                End If
            Next k
        Next j
    Next i
   
    For i = 1 To Nums
            s = s & Format(NUM(i), "0#") & " "
    Next i
    ''''''
    Text1.Text = s        't = timeGetTime - t      'MsgBox t
End Sub

非常感谢!不但可以从中学习编程知识,还能解决实际问题!
#5
独木星空2023-08-21 15:19
数字游戏,福利彩票,80选10(从80个号码选择20个号码,选民选10个,只要这10个在20个之中,就算选中了),有人能计算出选中概率吗?
#6
hxx10212023-08-21 17:49
我曾经用这个随机数,买8注选十的,就有一注中了8个号,所以说,机会很大,但就是比较烧钱。个人感觉,把80个号码全买完,肯定机会大一些,起码没有杀号,比起在彩票店买的机会更大一些吧
#7
约定的童话2023-08-22 07:35
这种适合退休金用户玩,年轻人没那个资本....
#8
独木星空2023-08-26 00:03
任何游戏都需要资本,不是拜金啊,这是现实。
#9
lianyicq2023-10-08 16:22
回复 5楼 独木星空
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
内容不能为空
#10
hqso2023-10-16 14:50
这玩意 可不能当事业搞
1