0

I recently started programming with SmallBasic and I was wondering if it was possible to check if a random number is already in an array. My code so far:

Count = 10
For i = 1 to Count
  var[i] = Math.GetRandomNumer(100)
  TextWindow.Write(var[i] + ‘,’)
Endfor 

Thanks in advance:)

2 Answers2

1

This seems to work well:

Count = 10

For i = 1 to Count+1
  If (duplicate) Then
    i = i - 1 
    duplicate = "False"
  EndIf

  var[i] = Math.GetRandomNumber(10)

  For u = 1 To i-1
    If var[i] = var[u] Then
      duplicate = "True"
      EndIf
    EndFor
  Endfor 


  For y = 1 To Count
    TextWindow.Write(var[y] + ", ")
  EndFor
Zock77
  • 951
  • 8
  • 26
0

This version will probably be the fastest. It takes advantage of the fact that arrays in Smallbasic are actually Maps and are stored in a string. This makes them very slow compared to array actions in other languages. However if you treat the array as a string some actions can take place with surprising speed.

numberList[0] = 0
For i = 1 To 100
  choice = 0
  While Text.IsSubText(numberList, "=" + choice + ";")
    choice = Math.GetRandomNumber(100)
  EndWhile
  numberList[i] = choice
EndFor

For i = 1 To  100
  TextWindow.Write(numberList[i] + " ")
EndFor
codingCat
  • 2,396
  • 4
  • 21
  • 27