News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How to Generate random number

Started by Farabi, October 22, 2012, 01:26:39 PM

Previous topic - Next topic

Farabi

Anyone know how to generate random number using any WinAPI?
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

qWord

MREAL macros - when you need floating point arithmetic while assembling!

dedndave

of course, you can also use something like QueryPerformanceCounter, too

interesting on the crypto functions - didn't know they were in there   :P

mineiro

Hello Sir Farabi;
The better definition that I have found about random numbers was after read the book "Algorithms" by Robert Sedgewick.
He say: "every number to be equally likely to occur", well, I think about sudoku in this time.
From my ceptical point of view, does not have an algo that generates random numbers, because if this is possible, they will not be random, instead of this, they will be predictable.
Sir Agner Fog have done some work about this.

Vortex

Hi Onan,

There is also the rand function exported by msvcrt.dll :

http://msdn.microsoft.com/en-us/library/aa272875(v=vs.60).aspx

TouEnMasm

There is a numerous number of post in this subject on the old forum.
There is just one thing to know:
There is pseudo random number generator.
You can recognize them runing them twice.The two series of numbers are the same.
There is real random number generator.
You can recognize them runing them twice.The two series of numbers are NOT the same.
In this sort of random generator you have to take care if there is repeat,no repeat,and wich numbers are generated.
For example , if you want a series of random numbers from 1 to 1000,you can generate a series of numbers from 100 to 500 and so on.

One thing is certain,never play money on a pseudo number generator.
The bank allways win.(there is just to change the algo from time to time)
You have enough answer here,to made a choice.






Fa is a musical note to play with CL

MichaelW

One problem with the CRT rand function is the 15-bit period:

#define RAND_MAX 0x7fff
Well Microsoft, here's another nice mess you've gotten us into.

bluedevil

Here is an implementation of creating randoms:
GetTickCount
RDTSC
nrandom & nseed
CryptGenRandom
Randomize,Random32 ve RandomRange (Irvine32 library)

I also wanted to add Rand() function from MasmBasic library but:
Quoteyou need either JWasm or at least ML.EXE version 6.15 to use the MasmBasic library; ML 6.14 (the version that is included with the Masm32 SDK) is not sufficient, because MasmBasic contains SSE2 code
So maybe jj2007 will help us for that  :icon_cool:
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

jj2007

Quote from: blue_devil on October 24, 2012, 07:17:08 AM
I also wanted to add Rand() function from MasmBasic library but:
Quoteyou need either JWasm or at least ML.EXE version 6.15 to use the MasmBasic library; ML 6.14 (the version that is included with the Masm32 SDK) is not sufficient, because MasmBasic contains SSE2 code
So maybe jj2007 will help us for that  :icon_cool:

I thought not having SSE2 was a problem of a tiny minority ::)

But it's a minor problem. The Rand macro is standalone and does not contain SSE2 code, so just extract the complete Rand MACRO from \masm32\MasmBasic\MasmBasic.inc, lines 3154...3352, and print str$(Rand(12345)) works with the standard Masm32 installation.

include \masm32\include\masm32rt.inc
include RandMacro.inc   ; to be extracted from \masm32\MasmBasic\MasmBasic.inc

.code
start:   xor ebx, ebx
   .Repeat
      print str$(Rand(123)), " "
      inc ebx
   .Until ebx>1000
   inkey "ok"
   exit
end start


By the way, rdtsc is a good initialiser but watch out for two traps that can influence the quality of the random figures generated:
- granularity: at least on my trusty Celeron, all values returned by rdtsc can be divided by 8 (see attachment)
- wraparound: every once in a while, rdtsc might return zero (!) or at least a value that sees only the lowest byte set.

bluedevil

Quote from: jj2007 on October 24, 2012, 08:07:09 AM

I thought not having SSE2 was a problem of a tiny minority ::)

But it's a minor problem. The Rand macro is standalone and does not contain SSE2 code, so just extract the complete Rand MACRO from \masm32\MasmBasic\MasmBasic.inc, lines 3154...3352, and print str$(Rand(12345)) works with the standard Masm32 installation.

include \masm32\include\masm32rt.inc
include RandMacro.inc   ; to be extracted from \masm32\MasmBasic\MasmBasic.inc

.code
start:   xor ebx, ebx
   .Repeat
      print str$(Rand(123)), " "
      inc ebx
   .Until ebx>1000
   inkey "ok"
   exit
end start


By the way, rdtsc is a good initialiser but watch out for two traps that can influence the quality of the random figures generated:
- granularity: at least on my trusty Celeron, all values returned by rdtsc can be divided by 8 (see attachment)
- wraparound: every once in a while, rdtsc might return zero (!) or at least a value that sees only the lowest byte set.

I have updated the sources as you mentioned jj2007, great thanks =) :icon_cool:
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

jj2007

Quote from: blue_devil on October 24, 2012, 08:34:30 AM
I have updated the sources as you mentioned jj2007, great thanks =) :icon_cool:

Thanks, it looks cute. The Rand() macro does a lot more than just returning dwords in eax, see here, but that is a longer story ;-)

Attached your inc file with minor modifications that allow to assemble your code with a standard Masm32 installation plus two files from the MasmBasic package:
\masm32\MasmBasic\irvinemb\Irvine32Mb.inc
\masm32\MasmBasic\irvinemb\irvine32Mb.lib

Farabi

http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

bluedevil

@jj2007 i have the last version of MASM32 package but ML.EXE is 6.14 version? Is this normal?
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

Vortex

Hi blue_devil,

The version of ml.exe supplied with the Masm32 package is 6.14, that's normal.

jj2007

Quote from: blue_devil on October 24, 2012, 07:34:11 PM
@jj2007 i have the last version of MASM32 package but ML.EXE is 6.14 version? Is this normal?

Absolutely normal: 6.14 is the only one that can be distributed, for copyright reasons. There are three workarounds:
- use JWasm (best option),
- Google for MASM "6.15" to find a download,
- use ML 8.0 from here (PM me for details - it's only 311k to download, but installation is tricky)
- use ML 9 or 10 from the Visual xyz packages, if you have them