我试图生成一个有十个变量的数组,每个变量包含一个从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
非常感谢任何帮助,谢谢。
答案 0 :(得分:0)
这会给你一个随机数
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
答案 1 :(得分:0)
你需要做的是:
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
答案 2 :(得分:0)
轰!明白了。 我创建了一个数字数组,从1到10,然后我扰乱了数字的顺序:
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