| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1990 人关注过本帖, 1 人收藏
标题:有80个随机数,怎样只给固定位置的数排列?
取消只看楼主 加入收藏
hxx1021
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2022-6-17
结帖率:100%
收藏(1)
已结贴  问题点数:20 回复次数:3 
有80个随机数,怎样只给固定位置的数排列?
有80个随机数,怎样只给固定位置的数排列?
例如只给1-10的随机数排列,给11-20、21-30、31-40、41-50、51-60、61-70、71-80的随机数排列,示例见图片,源码见附件。
80个不重复的随机数.rar (8.05 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
程序代码:
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
搜索更多相关主题的帖子: For 随机数 Next If Dim 
2023-08-19 16:11
hxx1021
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2022-6-17
收藏
得分:0 
目前只有随机数,求教进行排序的方法,最好能在源码上修改,谢谢!
2023-08-19 16:13
hxx1021
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2022-6-17
收藏
得分:0 
以下是引用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

非常感谢!不但可以从中学习编程知识,还能解决实际问题!
2023-08-20 13:18
hxx1021
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2022-6-17
收藏
得分:0 
我曾经用这个随机数,买8注选十的,就有一注中了8个号,所以说,机会很大,但就是比较烧钱。个人感觉,把80个号码全买完,肯定机会大一些,起码没有杀号,比起在彩票店买的机会更大一些吧
2023-08-21 17:49
快速回复:有80个随机数,怎样只给固定位置的数排列?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020920 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved