flat assembler
Message board for the users of flat assembler.

Index > Main > avx or sse get random numbers.

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1995
Roman 19 Jul 2025, 18:44
fasmw 1.73
How get random numbers ?
I want generate Brownian noise.
Post 19 Jul 2025, 18:44
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1728
Location: Toronto, Canada
AsmGuru62 19 Jul 2025, 22:16
- 32-bit or 64-bit? (there is a good and small x64 generator just discovered, passes Big Crush tests)
- in order to get good results:
1. generate unsigned 32-bit value
2. divide this value by 0x100000000 using FPU -- you will get a value in range from 0.0000 to 1.0000.
3. Using the value from step #2 you can now generate the values in any integer range (pseudocode, just use FPU for most of the code):
Code:
int InRange (int LOW, int HIGH)
{
    int     EXTENT = HIGH - LOW;
    int     PART   = ROUND (EXTENT * RANDOM_FROM_0_TO_1);

    return  PART + LOW;
}
    

Any generator will require a seed.
I usually use the function GetSystemTimeAsFileTime, it gives you a 64 bit value (FILETIME),
but the higher bits of the value almost never change, so you can use GetTickCount and XOR the returning EAX into higher bits of FILETIME.
Or, if your generator is 32 bit -- just use lower bits of FILETIME.
Post 19 Jul 2025, 22:16
View user's profile Send private message Send e-mail Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1728
Location: Toronto, Canada
AsmGuru62 19 Jul 2025, 22:24
There are also instructions RDRAND/RDSEED.
Post 19 Jul 2025, 22:24
View user's profile Send private message Send e-mail Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1995
Roman 19 Jul 2025, 22:47
RDTSC good Idea?
Post 19 Jul 2025, 22:47
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20723
Location: In your JS exploiting you and your system
revolution 19 Jul 2025, 23:10
Roman wrote:
RDTSC good Idea?
It depends upon the quality of randomness needed.

In general RDTSC is awful for randomness, but for a simple unimportant task it can be perfectly fine to use RDTSC.
Post 19 Jul 2025, 23:10
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1995
Roman 20 Jul 2025, 14:41
Code:
;RandSeed dd 0

add eax,287123
imul  edx,[RandSeed],08088405H
inc edx
mov   [RandSeed], edx
mul edx
;in eax=random number
    
Post 20 Jul 2025, 14:41
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1728
Location: Toronto, Canada
AsmGuru62 20 Jul 2025, 18:51
Probably, some testing of the sequence is needed to verify the "randomness".
Post 20 Jul 2025, 18:51
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.