I was interested in the difference of srand(), rand() and rand_s() functions, provided by msvcrt.dll. I noticed that rand_s() will not need to generate an additional seed value and that rand_s() covers a larger number range than rand(). This can be checked by running the code several times at the same time.
Along with the random generator functions already provided by Oxygenbasic, there are many ways to generate random numbers.
$ filename "Randomize.exe"
'uses rtl32
'uses rtl64
uses corewin
uses console
int err, x
uint number
'using rand_s function, will not need extra seed
cls
print "Randomizing using rand_s():"
printl
for x= 1 to 10
err = rand_s(&number) : if err != 0 then printl "rand_s function failed"
print number " "
next
printl
for x= 1 to 10
err = rand_s(&number) : if err != 0 then printl "rand_s function failed"
print mod(number, 100) " "
next
printl
printl "Randomizing using rand() without srand():"
printl
for x= 1 to 10
number = rand()
print number " "
next
printl
for x= 1 to 10
number = rand()
print mod(number, 100) " "
next
printl
printl "Randomizing using srand() and rand():"
'seed the number generator
srand(GetTickCount())
printl
for x= 1 to 10
number = rand()
print number " "
next
printl
for x= 1 to 10
number = rand()
print mod(number, 100) " "
next
printl
printl "Enter ..."
waitkey
Sample output:
Randomizing using rand_s():
2180143988 1982192448 3495921706 3833399829 3083074534 3074167681 3342874239 3908265420 313386393 1972157771
80 53 25 53 52 81 5 6 3 98
Randomizing using rand() without srand():
41 18467 6334 26500 19169 15724 11478 29358 26962 24464
5 45 81 27 61 91 95 42 27 36
Randomizing using srand() and rand():
22777 16830 2350 16983 27237 12752 11839 19604 21405 407
16 53 74 30 89 41 43 45 79 61