从 1 到 10 的不同数字 [英] Different numbers from 1 to 10

查看:38
本文介绍了从 1 到 10 的不同数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 0-9 的范围内生成 10 个不同的数字.所需的输出可能如下所示,9 0 8 6 5 3 2 4 1 7

I want to generate 10 different numbers from a range of 0-9. the desired output may look like this, 9 0 8 6 5 3 2 4 1 7

Dim arraynum(9) As Integer
Dim crmd As Boolean
Dim rmd as integer

For i = 0 To 9
    arraynum(i) = -1
Next i

crmd = True
Randomize Timer
For i = 0 To 9
    rmd = Int(Rnd * 10)
    For j = 0 To 9
        If arraynum(j) = rmd Then
            j = 9
            If crmd = False Then
                i = i - 1
            End If
            crmd = True
        Else
            crmd = False
        End If
    Next j
    If crmd = False Then
        arraynum(i) = rmd
        QuestionILabel.Caption = QuestionILabel.Caption + Str(arraynum(i))
    End If
Next i

推荐答案

选择随机值然后丢弃那些你已经使用过的值是一个糟糕的想法.随着可用数字池的减少,因为您丢弃的越来越多,它使运行时间变得更长.

Choosing random values and then throwing away those that you've already used is a bad idea. It makes the run-times longer as the pool of available numbers gets less since you're throwing away more and more.

您想要的是一个随机列表,我将使用以下代码(伪代码,因为它是家庭作业)来实现:

What you want is a shuffle list which I would implement with the following code (pseudo-code since it's homework):

dim n[10]                 // gives n[0] through n[9]
for each i in 0..9:
    n[i] = i              // initialize them to their indexes
nsize = 10                // starting pool size
do 10 times:
    i = rnd(nsize)        // give a number between 0 and nsize-1
    print n[i]
    nsize = nsize - 1     // these two lines effectively remove the used number
    n[i] = n[nsize]

通过简单地从池中选择一个随机数,然后用该池中的最高数字替换它并减小池的大小,您就可以进行洗牌,而不必预先担心大量的交换.如果数字很大,这很重要,因为它不会引入不必要的启动延迟.

By simply selecting a random number from the pool then replacing it with the top number from that pool and reducing the size of the pool, you get a shuffle without having to worry about a large number of swaps up front. This is important if the number is high in that it doesn't introduce an unnecessary startup delay.

例如,检查以下基准检查:

For example, examine the following bench-check:

<--------- n[x] ---------->
for x = 0 1 2 3 4 5 6 7 8 9  nsize  rnd(nsize)  output
---------------------------  -----  ----------  ------
        0 1 2 3 4 5 6 7 8 9     10           4       4
        0 1 2 3 9 5 6 7 8        9           7       7
        0 1 2 3 9 5 6 8          8           2       2
        0 1 8 3 9 5 6            7           6       6
        0 1 8 3 9 5              6           0       0
        5 1 8 3 9                5           2       8
        5 1 9 3                  4           1       1
        5 3 9                    3           0       5
        9 3                      2           1       3
        9                        1           0       9

您可以看到池随着使用而减少,并且因为您总是用未使用的替换使用过的,所以您永远不会重复.

You can see the pool reducing as you go and, because you're always replacing the used one with an unused one, you'll never have a repeat.

而且现在你的家庭作业包括把它变成 VB :-)

And now your homework consists of turning that into VB :-)

而且,由于这项作业现在几乎肯定会过期(大约一年前),为了完整起见,我将发布一个 VBA 解决方案,展示如何做.

And, since this homework is now almost certainly overdue (about a year ago), I'll post a VBA solution showing how to do it, for completeness.

Option Explicit
Option Base 0
Sub Macro1()
    Randomize
    Dim list(10) As Integer
    Dim i As Integer
    Dim size As Integer
    Dim pos As Integer
    Dim result As String

    For i = 0 To 9
        list(i) = i
    Next

    size = 10
    result = ":"
    For i = 1 To 10
        pos = Int(Rnd() * size)
        result = result & list(pos) & ":"
        size = size - 1
        list(pos) = list(size)
    Next

    MsgBox result
End Sub

这在三个单独的运行中生成:

This generated, on three separate runs:

:5:7:4:2:9:1:0:8:3:6:
:3:9:6:0:7:8:5:4:2:1:
:7:6:3:5:1:8:9:0:4:2:

这篇关于从 1 到 10 的不同数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆