数组中的随机数,Small Basic

Random Numbers in array, Small Basic

我正在尝试生成一个包含十个变量的数组,每个变量包含一个从 1 到 10 的随机数。随机整数不能在数组中出现多次。每次程序执行时,数组都会被重置为不同的数字。例如,在英语中,我想要的本质上是:

randint[1] = random number <= 10 and >= 1
randint[2] = random number <= 10 and >= 1 and <> randint[1]
randint[3] = random number <= 10 and >= 1 and <>  randint[2]
randint[4] = random number <= 10 and >= 1 and <>  randint[3]
randint[5] = random number <= 10 and >= 1 and <>  randint[4]
randint[6] = random number <= 10 and >= 1 and <>  randint[5]
randint[7] = random number <= 10 and >= 1 and <>  randint[6]
randint[8] = random number <= 10 and >= 1 and <>  randint[7]
randint[9] = random number <= 10 and >= 1 and <>  randint[8]
randint[10] = random number <= 10 and >= 1 and <>  randint[9]

简单地说,我正在尝试以随机顺序对数组中的数字 1 到 10 进行排序。

目前我的代码如下:

For i = 1 To 10
  While rand_int[i] = prev_int
    rand_int[i] = Math.GetRandomNumber(9)+1
    prev_int = rand_int[i]
  EndWhile
EndFor

我也试过

rand_int[1] = Math.GetRandomNumber(9)+1

for i = 2 To 10
  rand_int[i] = Math.GetRandomNumber(9)+1
  While rand_int[i] = rand_int[i-1]
    rand_int[i] = Math.GetRandomNumber(9)+1
  EndWhile
EndFor

如有任何帮助,我们将不胜感激,谢谢。

这会给你一个随机数

For i = 1 To 10 Step 1
  num[i]["num"] = i
  num[i]["chosen"] ="not chosen"
endfor
For i = 1 To 10 Step 1
  getnumber()
  endfor

Sub getnumber

  number=Math.GetRandomNumber(10)

  If num[number]["chosen"] = "not chosen" then
    TextWindow.WriteLine(num[number]["num"])

    num[number]["chosen"] = "chosen"
  Else 
    getnumber()
    endif
 EndSub 

你要做的是:

For i = 1 To 10
While randint[i] = randint[i-1] Or randint[i] = "" '<--- while the randint is equal to the last, it will keep generating numbers (Or while the randint is nothing)
randint[i] = Math.GetRandomNumber(10)
EndWhile
TextWindow.WriteLine(randint[i])
EndFor

轰!明白了。 我创建了一个数字数组,从一数到十,然后我打乱了数字的顺序:

For i = 1 To 10
  Randint[i] = i
EndFor

For i = 1 To 30 'The Larger the number, the more it scrambles the order
  Rand1 = Math.GetRandomNumber(10)
  Rand2 = Math.GetRandomNumber(10)
  OldRandint = Randint[Rand1] 
  Randint[Rand1] = Randint[Rand2] 'Here we are swapping two random varibles in the array
  Randint[Rand2] = OldRandint
EndFor

For i = 1 To 10
  TextWindow.WriteLine(Randint[i])
EndFor