flat assembler
Message board for the users of flat assembler.

Index > DOS > Flip a coin

Author
Thread Post new topic Reply to topic
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
Coddy41 05 May 2009, 18:23
In Fasm how do I make the computer randomly choose a number? like between 1 and 2?

_________________
Want hosting for free for your asm project? You can PM me. (*.fasm4u.net)
Post 05 May 2009, 18:23
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
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?

Wink
Post 05 May 2009, 18:37
View user's profile Send private message Visit poster's website Reply with quote
rCX



Joined: 29 Jul 2007
Posts: 172
Location: Maryland, USA
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. Cool

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
Post 05 May 2009, 18:57
View user's profile Send private message Reply with quote
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
Coddy41 05 May 2009, 18:57
Oh, thanks, I shall study it well Very Happy again thanks
@revolution: thank you I was looking for that name Smile
Post 05 May 2009, 18:57
View user's profile Send private message Visit poster's website Reply with quote
rCX



Joined: 29 Jul 2007
Posts: 172
Location: Maryland, USA
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 Smile) 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" Wink) 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
Post 05 May 2009, 19:16
View user's profile Send private message Reply with quote
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
Coddy41 05 May 2009, 19:20
So it is not REALLY random, it just is hard to predict right?
Post 05 May 2009, 19:20
View user's profile Send private message Visit poster's website Reply with quote
rCX



Joined: 29 Jul 2007
Posts: 172
Location: Maryland, USA
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...
Post 05 May 2009, 19:23
View user's profile Send private message Reply with quote
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
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 Smile

I have never been able to make a random number... I just think of a few numbers...
Post 05 May 2009, 19:39
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
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.
Post 11 May 2009, 12:54
View user's profile Send private message Reply with quote
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
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.
Post 11 May 2009, 13:53
View user's profile Send private message Visit poster's website Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
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.... Rolling Eyes

_________________
----> * <---- My star, won HERE
Post 11 May 2009, 14:15
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
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).
Post 11 May 2009, 14:20
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
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/
Post 11 May 2009, 14:45
View user's profile Send private message Reply with quote
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
Coddy41 11 May 2009, 14:54
I may have to get a prepaid card...
Post 11 May 2009, 14:54
View user's profile Send private message Visit poster's website 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.