How to create random number with machine code?

Here you can ask questions or provide insights about how to use efficiently 6502 assembly code on the Oric.
Post Reply
User avatar
peacer
Flight Lieutenant
Posts: 381
Joined: Wed Jun 09, 2010 9:23 pm
Location: Turkey
Contact:

How to create random number with machine code?

Post by peacer » Sun Dec 23, 2018 12:45 am

Can you help me how to create a random number with machine code?

I need 4 different cases when I call the routine. So what ı want is ;

For example.. As basic commands
CALL x
P=PEEK(0)

So, P will get 0,1,2 or 3 randomly from memory adress 0

To explain it briefly Its like P=INT (4*RND(0))

Thanks

User avatar
Symoon
Archivist
Posts: 1427
Joined: Sat Jan 14, 2006 12:44 am
Location: Paris, France
Contact:

Re: How to create random number with machine code?

Post by Symoon » Sun Dec 23, 2018 7:11 am

Short reply (sorry, in a hurry): I would read the timer's low byte and keep the 2 lower bits from it.

Something like
LDA $0308
then any other instruction to set the 6 bits to 0, giving 000000xy
(than would probably be AND #$03 but please double check, I have to leave now ;) )

then A = 0, 1, 2 or 3.
Store it in $00 with
STA $00

and don't forget to end the program with
RTS

User avatar
peacer
Flight Lieutenant
Posts: 381
Joined: Wed Jun 09, 2010 9:23 pm
Location: Turkey
Contact:

Re: How to create random number with machine code?

Post by peacer » Sun Dec 23, 2018 9:52 am

Thank you, short and easy :)

User avatar
Badger
2nd Star Corporal
Posts: 23
Joined: Sat Sep 22, 2018 10:04 am
Location: Wigan, England

Re: How to create random number with machine code?

Post by Badger » Sun Dec 23, 2018 10:05 am

I can confirm Symoon's answer is correct.

In basic you can just do :-
POKE 0,PEEK(#308)AND3

and in assembly

lda $0308
and #$03
sta $00
rts
flag_uk Amateurs built the Ark, Professionals built the Titanic.

User avatar
peacer
Flight Lieutenant
Posts: 381
Joined: Wed Jun 09, 2010 9:23 pm
Location: Turkey
Contact:

Re: How to create random number with machine code?

Post by peacer » Sun Dec 23, 2018 10:50 am

Thank you too :)

Post Reply