0

I'm trying to generate an array which would have ten variables, each containing a random number from 1 to 10. The random integer can not be in the array more than once. Each time the program would execute, the array would be reset with different numbers. For example, in English, what I would like is essentially:

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]

Simply, I am trying to sort the numbers 1 to 10 in a random order in an array.

So far my code is as follows:

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

I have also tried instead

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

Any assistance would be greatly appreciated, Thanks.

3 Answers3

0

this will give you a random number

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 
Matthew
  • 157
  • 1
  • 9
  • Thanks, I've just plugged this in although I'm not familiar with the step concept. I've just put 'textwindow.writeline(num[i])' into a for loop and it doesn't quite output what I would've expected. Please could you explain this? Thanks – Louis Segal Feb 07 '16 at 17:12
  • OK so the forlorn in the beginning first sets eacheck number 1-10. It then sets it to not chosen. Then in the next for loop it chosen a random number. If that number has not been chosen then it sets it and prints it. If it has been chosen then it will choose again for a number that hasn't or has been chosen. Hope that helps – Matthew Feb 08 '16 at 21:38
0

What you have to do is this:

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
Zock77
  • 951
  • 8
  • 26
  • Thanks, I've just tried this and it still seems to output some numbers more than once. – Louis Segal Feb 07 '16 at 17:09
  • Oh. I thought you wanted no number to be the same as the last. Not all numbers to be different. I will give that a shot. – Zock77 Feb 07 '16 at 18:04
0

Boom! Got it figured out. I created an array of numbers, that counted from one to ten, and then I scrambled the order of the numbers:

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
Zock77
  • 951
  • 8
  • 26