Random Number Generator

Discussion in 'Discussion and Q&A Archive' started by RandomAvatarFan, Apr 15, 2009.

Thread Status:
Not open for further replies.
  1. RandomAvatarFan

    RandomAvatarFan Well-Known Member Member

    Joined:
    Apr 10, 2009
    Messages:
    100
    As you could see from my title, I'm trying to get Sonic 1 to generate a random number before loading a level at the title screen or level select. It won't be completely random, I'd like to set parameters (between x number an y number.) but that's the general idea.


    I understand something similar was done in RHS's Sonic 2 recreation with the ? ring box, yet I have something very different in mind with what I'd like to do with it.


    If I can just get a little hint on how to do this in ASM (Hivebrain's 2005 disasm) it would greatly be appreciated.
     
  2. nineko

    nineko I am the Holy Cat Member

    Joined:
    Mar 24, 2008
    Messages:
    1,902
    Location:
    italy
    Sonic 1 already comes with a pseudo-random number generator.


    Which is, incidentally, the first result you get if you search for "random" in the asm file.



    Code:
    ; ---------------------------------------------------------------------------
    
    ; Subroutine to	generate a pseudo-random number	in d0
    
    ; ---------------------------------------------------------------------------
    
    
    
    ; ||||||||||||||| S U B	R O U T	I N E |||||||||||||||||||||||||||||||||||||||
    
    
    
    
    
    RandomNumber:
    
    		move.l	($FFFFF636).w,d1
    
    		bne.s	loc_29C0
    
    		move.l	#$2A6D365A,d1
    
    
    
    loc_29C0:
    
    		move.l	d1,d0
    
    		asl.l	#2,d1
    
    		add.l	d0,d1
    
    		asl.l	#3,d1
    
    		add.l	d0,d1
    
    		move.w	d1,d0
    
    		swap	d1
    
    		add.w	d1,d0
    
    		move.w	d0,d1
    
    		swap	d1
    
    		move.l	d1,($FFFFF636).w
    
    		rts	
    
    ; End of function RandomNumber
    To use it, look at how it's called by other game subroutines (just call it with jsr or bsr and d0 will contain a random number). I'm not familiar with it, but it should return a number between 0 and 255 iirc.
     
  3. Malevolence

    Malevolence Well-Known Member Member

    Joined:
    Jul 29, 2008
    Messages:
    97
    I believe it can return huge numbers as well (size of longwords). All you do is jsr RandomNumber and then you get a random number returned in d0. To cap it just use the command and.
     
  4. RandomAvatarFan

    RandomAvatarFan Well-Known Member Member

    Joined:
    Apr 10, 2009
    Messages:
    100
    Oh wow. :p I've been looking at guides to try and write a subroutine myself, and here it is already in the ASM. I feel really noobish right now, thanks nineko.


    I think I can take it from here.
     
Thread Status:
Not open for further replies.