flat assembler
Message board for the users of flat assembler.
Index
> DOS > Flip a coin |
Author |
|
revolution 05 May 2009, 18:37
You buy a hardware random number generator, plug it in, install the driver, write some code to get the random bits and then you have your answer.
Or perhaps you meant pseudo-randomly? |
|||
05 May 2009, 18:37 |
|
rCX 05 May 2009, 18:57
RND is a simple "pseudo" random number generator I use. It was posted on this board awhile back. There is another thread about this is here. I think Dex has a random number generator in his library as well.
This program spits out either a 0 or a 1 when the user presses a key. Pressing Esc quits. Code: org 100h use16 ;Set seed to timer so that the generator gives a differnt sequence every time. This is good practice but not necessary. mov ah,0 ;Function 0h int 1Ah ;cx:dx = clock ticks since midnight; al = Midnight flag ;PIT timer might be better mov word [Seed],dx ;Seed = clock ticks Again: call RND ;al = random number from 0 to 255 and al,00000001b ;al = random number from 0 to 1 because we cleared the upper bits add al,'0' ;al = either a '0' or '1' character mov dl,al ;dl = either a '0' or '1' character mov ah,02h ;\Print character in dl int 21h ;/ mov ah,0 ;\Wait for key int 16h ;/ cmp al,1Bh ;\If key is not Esc then do it again jne Again ;/ ret RND: ;Input: Seed = Random Seed; Output: al = pseudo random number between 0 and 255, Seed = New seed ;Based on RAND.INC from NASM pushf push dx push ax mov ax,[Seed] ;ax = Seed mov dx,8405h ;dx = 8405h mul dx ;dx:ax = 8045h*Seed cmp ax,[Seed] ;\IF new Seed = old Seed THEN alter seed jne .GotSeed ;/ mov ah,dl inc ax .GotSeed: mov [Seed],ax ;We have a new seed, so store it pop ax mov al,dl ;Return random number in al pop dx popf ret Seed dw 3749h Last edited by rCX on 05 May 2009, 18:58; edited 1 time in total |
|||
05 May 2009, 18:57 |
|
Coddy41 05 May 2009, 18:57
Oh, thanks, I shall study it well again thanks
@revolution: thank you I was looking for that name |
|||
05 May 2009, 18:57 |
|
rCX 05 May 2009, 19:16
I'm not an expert but here goes....
PRNGs are initialized by a "Seed" and generates a sequence of random numbers based on it. Every seed is associated with a specific sequence. For example if the initial seed was always 3749h, this program will always give... Code:
01110110100011...
By setting the seed to the clock (or a random number you came up with ) you can get a different sequence everytime the program is ran. Basically all RND does is multiply the seed by 8405h. From the result in dx:ax, dl is returned as a kinda- (hence "pseudo" ) random number and ax is saved as a new seed. To avoid repetition, if the new seed is the same as the old one, it simply increments it. Last edited by rCX on 05 May 2009, 19:27; edited 5 times in total |
|||
05 May 2009, 19:16 |
|
Coddy41 05 May 2009, 19:20
So it is not REALLY random, it just is hard to predict right?
|
|||
05 May 2009, 19:20 |
|
rCX 05 May 2009, 19:23
Yup. "Real" random numbers can only be made in the "real" world. And even this might not be true as well...
|
|||
05 May 2009, 19:23 |
|
Coddy41 05 May 2009, 19:39
41.36542 TB of code later (compiled)... I finally got it to to produce a somewhat random number!! jk
I have never been able to make a random number... I just think of a few numbers... |
|||
05 May 2009, 19:39 |
|
baldr 11 May 2009, 12:54
Coddy41,
Do yourself a favor, read volume 2 ("Seminumerical Algorithms") of THE BOOK ("The Art of Computer Programming" by D. E. Knuth). There is (along with other useful info, too) deep analysis of PRNGs. |
|||
11 May 2009, 12:54 |
|
Coddy41 11 May 2009, 13:53
I would like to, But, no book store neer here caries programing books, and I just bought a book on line, my
parents do not like to put there credit card number on the web. |
|||
11 May 2009, 13:53 |
|
windwakr 11 May 2009, 14:15
If you dont mind doing something bad(illegal), try a google search that searches just for file types you want:
Quote: intitle:"index.of" (pdf) file.name.here -asp -htm -html -cf -jsp you just replace file.name.here with whatever, you can change the pdf to anything too. Easy way to find file indexes with mp3s and things like that. If the name has spaces use dots like in "file.name.here" I just tested with that particular book and hit a few matches right away....I've said too much.... |
|||
11 May 2009, 14:15 |
|
Madis731 11 May 2009, 14:20
In Estonia banks offer a service called "virtual credit card". What it does is temporarily (say a year or 3 months) create a totally new credit card with its own number and amount of credit etc. but it is linked to your real credit card.
That is the suggested way of online-shopping. When your credit-info is stolen, then nothing bad can happen because this credit-cards info is only temporary and the credit-limit is lets say $49.99 and you already bought a book that cost you $49.99 so everybody wins (except the bad guys). |
|||
11 May 2009, 14:20 |
|
windwakr 11 May 2009, 14:45
Or, here in the U.S. you can go out and buy a prepaid refillable credit card.
Info on them here: http://www.mastercard.com/us/personal/en/aboutourcards/gift_prepaid/ |
|||
11 May 2009, 14:45 |
|
Coddy41 11 May 2009, 14:54
I may have to get a prepaid card...
|
|||
11 May 2009, 14:54 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.