Author Topic: Generating Random Numbers  (Read 2008 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Guest
Generating Random Numbers
« on: January 08, 2017, 12:42:50 AM »
Hello,

in the code of the Oxygen examples and include files several ways of generating random numbers are offered, so this is only another approach.

I noticed that the functions rand and srand provided with Windows can also be used to generate random numbers (up to 32767). Perhaps somebody else will find this method interesting too.

Roland

Code: OxygenBasic
  1. ' Simple random number generator (number max 32767)
  2. ' https://msdn.microsoft.com/en-us/library/aa272875(v=vs.60).aspx
  3.  
  4. $ filename "Randomizer.exe"
  5.  
  6. '#include "$/inc/RTL32.inc"
  7. '#include "$/inc/RTL64.inc"
  8.  
  9. #include "$/inc/console.inc"
  10.  
  11. ! GetTickCount lib "kernel32.dll" alias "GetTickCount" () as dword
  12. ! srand lib "msvcrt.dll" (int seed)
  13. ! rand lib "msvcrt.dll" () as int
  14.  
  15. % RAND_MAX = 32767
  16.  
  17. ' To seed the pseudorandom-number
  18. sub Randomize()
  19.    srand GetTickCount()
  20. end sub
  21.  
  22. SetConsoleTitle "Randomizer"
  23.  
  24. Randomize()
  25.  
  26. printl "Pseudo random number generation (max 32767):" & cr & cr
  27. int i
  28. for i = 1 to 50
  29.    int num = rand(): print num "  "
  30. next i
  31. printl & cr
  32.  
  33. printl "Random numbers between 1 and 49:" & cr & cr
  34. for i = 1 to 50
  35.    int num = mod(rand(),49)+1 : print num "  "
  36. next i
  37. printl & cr
  38.  
  39. printl "Random numbers between 0.0 and 1.0" & cr & cr
  40. for i = 1 to 50
  41.    double num = mod(rand(),10000)/10000 : print str(num,4) "  "
  42. next i
  43. printl & cr
  44.  
  45. printl "Random numbers between -100 and 100:" & cr & cr
  46. for i = 1 to 100
  47.    int num = rand()/(RAND_MAX+1)*(100+100) - 100 : print num "  "  
  48. next i
  49. printl & cr
  50.  
  51. print "Enter ... " : waitkey
  52.  

« Last Edit: June 14, 2019, 02:56:20 AM by Arnold »

Arnold

  • Guest
Re: Generating Random Numbers
« Reply #1 on: June 14, 2019, 03:02:40 AM »
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.

Code: [Select]
$ 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
« Last Edit: June 15, 2019, 12:45:48 AM by Arnold »