flat assembler
Message board for the users of flat assembler.

 Index > DOS > Random number generation Goto page 1, 2  Next
Author
typedef

Joined: 25 Jul 2010
Posts: 2908
Location: 0x77760000
typedef
Hie, typedef again

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

thanks.
12 Sep 2010, 14:58
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 18170
revolution

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?
12 Sep 2010, 16:01
typedef

Joined: 25 Jul 2010
Posts: 2908
Location: 0x77760000
typedef
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
12 Sep 2010, 16:29
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 18170
revolution
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
12 Sep 2010, 16:36
typedef

Joined: 25 Jul 2010
Posts: 2908
Location: 0x77760000
typedef
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
12 Sep 2010, 16:44
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 18170
revolution

http://en.wikipedia.org/wiki/Linear_congruential_generator
12 Sep 2010, 16:47
baldr

Joined: 19 Mar 2008
Posts: 1651
baldr
typedef,

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

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

Why do Brin & Page bother to create Google? Nobody seems to use it!
12 Sep 2010, 16:50
typedef

Joined: 25 Jul 2010
Posts: 2908
Location: 0x77760000
typedef
baldr wrote:
typedef,

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

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
12 Sep 2010, 20:22
revo1ution

Joined: 04 Mar 2010
Posts: 34
Location: somewhere, twiddling something
revo1ution
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. 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.
12 Sep 2010, 23:35
typedef

Joined: 25 Jul 2010
Posts: 2908
Location: 0x77760000
typedef
thanks rev.
13 Sep 2010, 00:48
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 18170
revolution
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!
13 Sep 2010, 01:39
typedef

Joined: 25 Jul 2010
Posts: 2908
Location: 0x77760000
typedef
Doesn't that require also a timer procedure? To avoid using too much CPU, like 32 bit GUI apps? lol.....I need to study more on Timer Ints.
13 Sep 2010, 02:09
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 18170
revolution
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.
13 Sep 2010, 02:19
bitRAKE

Joined: 21 Jul 2003
Posts: 3282
Location: vpcmipstrm
bitRAKE
This thread is cryptically called RANDOM - just ignore that though: it still might be helpful to you.
13 Sep 2010, 03:32
baldr

Joined: 19 Mar 2008
Posts: 1651
baldr
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
end if
mov     [random_seed], eax
ret

.data
random_seed    rd      1
```
13 Sep 2010, 05:03
revo1ution

Joined: 04 Mar 2010
Posts: 34
Location: somewhere, twiddling something
revo1ution
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?

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.
13 Sep 2010, 05:47
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 18170
revolution
Sorry that this is in C. I hope you can convert it to ASM without too much difficulty:

13 Sep 2010, 06:47
revo1ution

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

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
13 Sep 2010, 08:35
edfed

Joined: 20 Feb 2006
Posts: 4252
Location: Now
edfed
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.
13 Sep 2010, 08:55
DJ Mauretto

Joined: 14 Mar 2007
Posts: 464
Location: Rome,Italy
DJ Mauretto
Hello
Here there is a simply RNG test tool....

_________________
Nil Volentibus Arduum

Last edited by DJ Mauretto on 18 Nov 2011, 20:24; edited 1 time in total
20 Sep 2010, 09:39
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area
Goto page 1, 2  Next

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum