News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Unique random numbers

Started by jj2007, October 27, 2017, 10:00:36 PM

Previous topic - Next topic

jj2007

Probably another useless function, but from time to time the question pops up as an exercise in "high level" language forums, so I thought it might be nice to have it ;)

include \masm32\MasmBasic\MasmBasic.inc         ; download
include \Masm32\MasmBasic\Res\RandUnique.mac    ; included in MasmBasic.inc since 29 Oct 2017
  Init
  tests=20
  Dim MyRand() As DWORD
  PrintLine Str$("Possible range: %i ... ", -tests/2), Str$(tests/2)
  Let edi="unique numbers: "
  Rand()                                        ; set a seed value
  For_ ecx=0 To tests-1
        Rand(-tests/2, tests/2+1, MyRand(ecx), unique)  ; min, max, destination, flag
       Let esi=Str$(" %i ", MyRand(ecx))
        .if Instr_(edi, esi)
                inc  ebx                ; oops, we got that one already
        .else
                Let edi=edi+esi         ; remember the last number
        .endif
  Next
  if tests le 100
        PrintLine edi                   ; show the unique numbers
  endif
  Inkey Str$("-- %i errors, hit any key --", ebx)
EndOfCode


Output:Possible range: -10 ... 10
unique numbers:  -4  5  -9  -5  10  -2  -7  -3  2  -8  -6  8  4  6  9  3  -1  1  7  -10
-- 0 errors, hit any key --