flat assembler
Message board for the users of flat assembler.

Index > DOS > Random number generation

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 12 Sep 2010, 14:58
Hie, typedef again

Just wanted to know how i would generate a random number in DOS (16 bit). Just using pure ASM.

thanks.
Post 12 Sep 2010, 14:58
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20306
Location: In your JS exploiting you and your system
revolution 12 Sep 2010, 16:01
It ain't so simple to answer your broad and vague question so I have to ask questions in return:

What type of random numbers? What distribution? Do they need to cryptographically secure? Do you need to repeat the sequence? Do you have a good seed already? At what rate do you need to generate? Do you really mean pseudo-random numbers?
Post 12 Sep 2010, 16:01
View user's profile Send private message Visit poster's website Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 12 Sep 2010, 16:29
Just simple numbers; Integers, Floats, Long, Double etc
Pseudorandom I think would work fine, though I would also like to know how to just randomize

Here's an example in c++

Code:

int RandDomNumber = rand() % 100;

    


it will only generate a number between 1 and 100 only.

How would that be done in PURE ASM
Post 12 Sep 2010, 16:29
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20306
Location: In your JS exploiting you and your system
revolution 12 Sep 2010, 16:36
So you want: Integers, uniform distribution, non-crypto-secure, repeatable, seed unknown, high speed (poor quality), pesudo-random numbers?

rand() is a simple LCG. Fast and stupid.

x1 = x*A + C mod 2^16 (or mod 2^32), where A and C are constants and x is the previous value, x1 is the next value
Post 12 Sep 2010, 16:36
View user's profile Send private message Visit poster's website Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 12 Sep 2010, 16:44
Quote:
x1 = x*A + C mod 2^16 (or mod 2^32), where A and C are constants and x is the previous value, x1 is the next value


so that would sum up to this right?

Code:

;Start


MOV [rPrev],1


rNumber dd ?
rPrev dd ?
rNext dd ?

    


NOTE : I havent tried this code yet

pls help
Post 12 Sep 2010, 16:44
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20306
Location: In your JS exploiting you and your system
revolution 12 Sep 2010, 16:47
Wikipedia has a bit more info if you just want LCG.

http://en.wikipedia.org/wiki/Linear_congruential_generator
Post 12 Sep 2010, 16:47
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 12 Sep 2010, 16:50
typedef,

You need to refresh your C knowledge, rand() % 100 ∈ [0, 99]. Wink

TAOCP vol. 2 starts with chapter 3 — "Random Numbers". Enough said.

Why do Brin & Page bother to create Google? Nobody seems to use it!
Post 12 Sep 2010, 16:50
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 12 Sep 2010, 20:22
baldr wrote:
typedef,

You need to refresh your C knowledge, rand() % 100 ∈ [0, 99]. Wink

TAOCP vol. 2 starts with chapter 3 — "Random Numbers". Enough said.

Why do Brin & Page bother to create Google? Nobody seems to use it!


I said between 1 and 100 which means not over 100

Ok i was wrong for putting 1.

And what's the point of having forums if all you want to say is "use Google"

Who said that i never used Google. Please, lets help each other. Your problems may not be the same as mine. Learn to appreciate the presence of others.
Thank you
Post 12 Sep 2010, 20:22
View user's profile Send private message Reply with quote
revo1ution



Joined: 04 Mar 2010
Posts: 34
Location: somewhere, twiddling something
revo1ution 12 Sep 2010, 23:35
typedef:
This very simple routine will give you a (pseudo) random unsigned 8-bit integer which you can then process into whatever form you want. I can help further with this if you want.
Code:
push 0
pop ds
mov al,[46ch]
    


It reads the least significant 8 bits of the 32-bit hardware clock-tick counter driven from a 1.8MHz clock (if I remember correctly), which gets written to the BIOS data area at 0:46Ch (aka 40:6Ch).
It will work in any OS which allows access to low RAM including DOS of course, and will also work inside a bootloader pre-boot environment since it only needs the BIOS.

Don't be discouraged by the unhelpful or obscure answers you might get from this forum. I sometimes think an individual's helpfulness is inversely proportional to their number of posts. Wink But there are many exceptions to this including Tomasz of course. Note that I have made very few posts so far so I am very helpful. Laughing
Post 12 Sep 2010, 23:35
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 13 Sep 2010, 00:48
thanks rev.
Post 13 Sep 2010, 00:48
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20306
Location: In your JS exploiting you and your system
revolution 13 Sep 2010, 01:39
revo1ution: Reading a clock counter generates extremely poor pseudo-random numbers. Even using a clock as a seed it is dubious enough but directly using it for generation ... ugh!
Post 13 Sep 2010, 01:39
View user's profile Send private message Visit poster's website Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 13 Sep 2010, 02:09
Doesn't that require also a timer procedure? To avoid using too much CPU, like 32 bit GUI apps? Rolling Eyes lol.....I need to study more on Timer Ints.
Post 13 Sep 2010, 02:09
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20306
Location: In your JS exploiting you and your system
revolution 13 Sep 2010, 02:19
typedef: revo1ution's suggestion is unlikely to be helpful to you. I can't imagine a usage where reading the low frequency timer will generate a suitably random sequence. Even a low quality generator (like an LCG) will give you superior results.
Post 13 Sep 2010, 02:19
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4024
Location: vpcmpistri
bitRAKE 13 Sep 2010, 03:32
This thread is cryptically called RANDOM - just ignore that though: it still might be helpful to you.
Post 13 Sep 2010, 03:32
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 13 Sep 2010, 05:03
typedef,

I'm sorry if my post seemed rude to you, that wasn't intended.

Sentence about % operator simply corrects typical off-by-one mistake. It had smile attached, too.

"The Art of Computer Programming" by D. E. Knuth is the invaluable source of information, mentioned chapter discusses LCG in depth.

Simple Google search gives links to Wiki article about PRNGs and Agner Fog's page with Mersenne Twister/SFMT sources.

For me, your original post looks like "I want you to implement some PRNG algorithm in assembly for me". LCGs aren't so hard to program:
Code:
RANDOM_MULTIPLIER = 22695477
RANDOM_INCREMENT = 1

             .code
random_dword:      imul    eax, [random_seed], RANDOM_MULTIPLIER
if RANDOM_INCREMENT = 1
            inc     eax
else
         add     eax, RANDOM_INCREMENT
end if
             mov     [random_seed], eax
          ret

             .data
random_seed    rd      1
    
Post 13 Sep 2010, 05:03
View user's profile Send private message Reply with quote
revo1ution



Joined: 04 Mar 2010
Posts: 34
Location: somewhere, twiddling something
revo1ution 13 Sep 2010, 05:47
revolution:
As a very broad generalisation what you say is true, but if you are careful the code I posted can give very good results.
For example:
1. if the counter is read at a much lower average frequency than the counter clock frequency. The lower the better, because the higher the multiple, the lower is the (already small) chance of striking an exact integer multiple

2. if the counter is read asynchronously or, better still, intermittently such as being initiated by user input rather than within a regular program loop etc.

3. if only the fewest possible number of least significant bits are used - the fastest changing ones. You could even reduce it from 8 to 7 or 6 etc. by masking off the higher bits.

Why make something complicated when it could be very easy? Rolling Eyes

typedef:
It depends what you want, but my suggestion COULD be all you need. Address 0:46Ch can be read ANY time. You don't need to worry about the actual timer interrupt producing it, and it might be better NOT to incorporate it into your own timer-driven interrupt service routine because of reason 2. above. But again, as always it depends what you want. Wink
Post 13 Sep 2010, 05:47
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20306
Location: In your JS exploiting you and your system
revolution 13 Sep 2010, 06:47
Sorry that this is in C. I hope you can convert it to ASM without too much difficulty:

Image
Post 13 Sep 2010, 06:47
View user's profile Send private message Visit poster's website Reply with quote
revo1ution



Joined: 04 Mar 2010
Posts: 34
Location: somewhere, twiddling something
revo1ution 13 Sep 2010, 08:35
Well really revolution, before you were generalising. Now you're just being generally silly, while increasing your post-count Wink

Code:
random_number_generator_always_4:
;uses ax
mov ax,4
    

Very useful indeed!
mov al,[046Ch], or even mov ax,[046Ch] is infinitely superior Rolling Eyes
Post 13 Sep 2010, 08:35
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4330
Location: Now
edfed 13 Sep 2010, 08:55
random generator can be created with a simple white noise.
a white noise is the presence of all frequencies in a single signal.
a white noise can be created (in computers) by the addition of many ramps.
the instruction INC AL generates a ramp of frequency (f) of repetition(F)/256.
INC AX generates 2 ramps, one for AL, and one for AH, the ramp in AH is 256 times slower than the one in AL.
SHR and SHL instructions will divide by 2 the frequencies in the ramps
ROR and ROL will roll over the spectrum, and will act like a frequency mirror.
XOR, AND, ADD, SUB, DIV, MUL, and all arythmetic and logical instructions will have an effect on the randomness.

by xoring two ramps of different frequencies, we will do many more than just add them.
the fact that integers in computers are in limited dynamics (8 bit = 256 values) will induce randomness.



if you want to code a good random generator, first code a function to see these random numbers.
because the eye will be the first tool able to see randomness or not.

an example of random image is the snow in the TV. it is picked from white noise in the athmosphere.
if your program displays something like the TV snow, it have great chances to be random.

if it generates some repetitive patterns, it have a lot of chance to don't be random.




when you use random in a game, the repetitive patterns will be very annoying because you will see the same actions in a loop. but sometimes, it is funny to have a pseudo random generator.
Post 13 Sep 2010, 08:55
View user's profile Send private message Visit poster's website Reply with quote
DJ Mauretto



Joined: 14 Mar 2007
Posts: 464
Location: Rome,Italy
DJ Mauretto 20 Sep 2010, 09:39
Hello Wink
Here there is a simply RNG test tool....

_________________
Nil Volentibus Arduum Razz


Last edited by DJ Mauretto on 18 Nov 2011, 20:24; edited 1 time in total
Post 20 Sep 2010, 09:39
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.