Somebody a while back posted a method to generate a random number before that was quite a short peice of code. I need something like that to shuffle a deck of just three cards. I have some of the stuff others posted but i'm a little inexperienced and find the code a little complex.

All i want is number thrown into eax and then i check al and decide what card ID to push on the stack with this.

.IF al > 200
mov ebx,23
push ebx
.ELSEIF al <100
mov ebx,24
push ebx
.ELSE
mov ebx,25
push ebx
.ENDIF

Any help with a short method appreciated.
:alright:
Posted on 2001-09-10 17:52:29 by titan
Hi Titan.

You can use this formula taken from the book "Master Class: Assembly Languge":

R2 = ((R1 * X) + 1) mod Y

where
R1 = result or previous R2
R2 = the next number in the sequence
X = constant
Y = constant

Y should be according to the CPU word size (eg, 65536 for 16 bits)
The value for X should not be too large or small compared to Y, should be one less digit less than Y, and with no particular pattern except that it should end in ...21.

Example values :

Y = 65526
X = 9421

Since Y is aparently used to keep the result within 16 or 32 bit bounds (or whatever your word size) I will not use it in my example, as the CPU will do the work for me.

Example MASM code, useing the formula:

R2 = (R1 * X) + 1

.data
Seed dw 0; initialize from system clock, timer, etc

.code

;return RND value in AX
GetRND proc near uses bx dx

mov ax,Seed
mov bx,9421
mul bx
inc ax

ret

GetRND endp

Latter...
Posted on 2001-09-10 18:32:48 by CarlosM7
thanx...i'll check that out:alright:
Posted on 2001-09-10 18:51:12 by titan
I posted this: Random Macro for you last time.. and it does exactly what you want, while still being a very good # generator.

If you find macro's hard to work with.. all you need to do is cut&paste both the RAND32 and the SWAP into your project file, or any other file included in your project and its ready to go.

Even better, cut & paste them into a separate file and save them as Random32.inc, then all you need to do is include this file.

To get a random # in eax:
``````
mov eax, RAND32(3)  ; 3 cards to choose from
; now eax will have a random # from 0 -> 2....

.IF( eax == 2 )
mov ebx,23
push ebx
.ELSEIF( eax ==0 )
mov ebx,24
push ebx
.ELSE      ; must be a 1!
mov ebx,25
push ebx
.ENDIF``````

This conforms to your first post, but since eax is random anyways, an even better coding organization would be:
``````
push eax
``````

RAND32() is a macro, which means it will be replaced (in this case) by "eax". IE) "RAND32()" --> "eax". Befor this happens all the random algorithm is also stuck in your code just before where RAND32() was found... the result would look like this:
``````
...
(( Your code.. whatever it is... ))

(( Random code found in Macro ))
(( At the end,  eax == RAND#   ))
add eax, 23        ; "Rand32()" is replaced by "eax"
push eax            ; push it. will be either 23, 24, or 25!
...``````

I hope this helps you understand it.. its actually quite simple...

NaN
Posted on 2001-09-10 23:56:12 by NaN
Nan -

wow! I wrote a random number generator too in C on a 386 back in 1992, based on the built-in clock as the seed.

The more things change the more they go downhill at a very fast rate, whatever that means. It's 2:00 am
Posted on 2001-09-11 00:54:52 by drarem
``````
mov ebx,23
cmp  al,100
adc   ebx,0 ;if al < 100 ebx = 24 else 23
cmp al,200
adc ebx,0   ;if al < 200 and < 100 ebx = 25; if al < 200 and >=100 ebx = 24
push ebx   ;if ebx >= 200 ebx = 23
``````
Posted on 2001-09-11 08:11:23 by The Svin
i copied them all

should have my prog working in no time
:alright:
Posted on 2001-09-11 11:50:48 by titan
NAN i got your macro working in my program. The only problem is it works too well. The idea in my program is to have three symbols displayed. If the three symbols all match the user wins something like money etc. (Its a game.) What i mean by your macro working too well is that it will never display the same three symbols all at once because it IS so random. I thought a way of getting more matches would be to generate say 36 random numbers and then choose every twelve one but this might end up doing the same thing. If you or any others that read this have any ideas how to make this work i'm all ears. Since I would want the user to win at least 1 out of 8 tries the algo would probably not be totally random. I clicked to try and get a match of three identicle symbols about three hundred times with no luck.

Yah that macro is definitly RAND32

thanx for the help:alright:
Posted on 2001-09-12 18:07:15 by titan
The quickest solution is to only have two symbols - if they were totally random, then the player would win 1 out of 8. If you want to add more dynamics to the game and keep the same odds, then you could just randomize the symbol if they don't match. ;)
Posted on 2001-09-12 18:36:16 by bitRAKE
I would do what your first solution is... (your quickly moving into the realm of statistics tho..)

Since your happy the generator is Random, the probability of any value out of 3 is : 1/3, and the probablility of NOT getting this value is 2/3.

Now your demo of having the user get a win by getting a match of 3 separate random #'s, in order is a Binomial sytle problem in general.

Probablitly of 3 matches(x) in 3 random events(n):

P(x) == (n Choose x)*[(1/3)^(x)]*[(2/3)^(n-x)]

where (n Choose x) is a function on any decient calc. "nCx". Here you have 3 events to choose from, and you want 3 exact matches in each.. hence only 1 specific possibility of this, getting the right match in order.. So this is replaced by 1. (try it on calc if you dont believe me :)

Since n == x, the second term's exponent goes to 0, and the term itself goes to 1. This is a specail case of the binomial, where the Probability is simply (1/3)^3... or 1/27 chance of exactly 3 numbers in the same order. As there is only 27 ways you can re-arrange 3 numbers.

111, 112, 113 - 121, 122, 123 - 131, 132, 133
211, 2xx, 2xx ............................
311, 3xx, 3xx ...........................

Now to increase to 36 numbers, and group in 12's...

Start at square 1: P of #: 12/36 == 1/3

The # of tries will remain the same (3), and the # of exact matches out of these tries as well will be (3). So the same binomail exists... 3 #'s, choos 3 exact matches == 1*(1/3)^3

Same probability of 1/27...

The thing to remember is there is NO true random # generator in a purely finite maching.. (CPU). So it is possible to have these stray... as your saying it was far higher... ( 100's seems questionably high, but anyways).

As well, 1/27 is only the THEORETICAL value, and is only found by averaging 1000's of tries.. 27 :: 100 is ~ 3 so this is not a fair attempt of showing this average. 27:10000 is, and would be a fair average if i got 350 exact matches in 10000 trials. The average would be 350/10000 = 1/ 28.57142, and here is a good responce.

Anywho.. this is a quick tutorial on probabilitys and simple stats... (i personally dont like to tread down these roads, but since your looking for info... :) )

:alright:
NaN
Posted on 2001-09-12 20:30:05 by NaN
NaN that was an excellent post. I have taken courses in finite mathematics, statistics and calculus, and the post really brought to mind all those permutations,combinations,arrangments,standard deviations, derivatives and the like.

You just made me realize that i havn't focused on that stuff for a long time and it is kind of ironic when they are the very tools that ultimately make a programmer (or for that matter an atomic physicist) good. I would try your suggestion of trying it on the calc but the the "nCr" button don't function so you can't do any binomail stuff. I suspected the battery was low but it is odd all other functions work. maybe blew a chip.

Bit rake...thanx also
Posted on 2001-09-12 23:03:22 by titan

There is a factorial expansion of the nCx, if you wanted to do it by hand...

Say 20 Choose 5, (20C5), would be:
``````
n!           20!         20*19*18*17*16      1860480
nCx = ---------- = --------- = ------------------- = -------- = 15504
x!*(n-x)!     5!(15!)           5!              120
``````
PS: Some calculators like to use the x<->y button with 'n' in x, and 'x' in y.

Anywho.. happy coding...

:alright:
NaN
Posted on 2001-09-12 23:21:36 by NaN