|
Random
Apr 18, 2015 9:50:15 GMT -5
Post by cNp on Apr 18, 2015 9:50:15 GMT -5
Can anyone confirm or deny my understanding that the BIOS Random function returns a value from -128 to +128 ?
Cheers,
Chris
|
|
|
Random
Apr 18, 2015 11:32:10 GMT -5
cNp likes this
Post by mikiex on Apr 18, 2015 11:32:10 GMT -5
AFAIK it makes a random byte, depending on how you interpret that it could be 0 to 255 or -128 to +127 (or any other form that's up to you)
A separate issue is how good a random number generator is it for your chosen use? If you want a uniform distribution of random numbers (I found the bios routine not good for this)
|
|
|
Random
Apr 20, 2015 4:33:12 GMT -5
Post by cNp on Apr 20, 2015 4:33:12 GMT -5
Cheers MikieX.
I'm using Random and comparing the number it generates to a value I'm storing in A register... using -127 to +127 seems to work...
I'm using it to set the location of a game item in 1 of 5 locations so it's ok for this use... although it obviously works off some internal timer because if there's no user input to when the game starts (I had commented out the title page, etc.) it almost always gave the same result!
cNp
|
|
|
Random
Apr 21, 2015 2:28:19 GMT -5
cNp likes this
Post by mikiex on Apr 21, 2015 2:28:19 GMT -5
I don't think there is any timer, if you look at the bios source code it messes with 3 bytes (The seed) of ram every time random is called, most likely that memory is always the same (at least on a hard reset?). So if that ram is the same you will always get the same sequence.
I don't know if there is a good source of a seed on the vectrex? - anyone?
|
|
|
Random
Apr 21, 2015 12:39:15 GMT -5
Post by mikiex on Apr 21, 2015 12:39:15 GMT -5
I was thinking you could run your own timer on the start screen and use that to seed when you press the button to start the game.
|
|
|
Random
Apr 22, 2015 2:13:42 GMT -5
Post by cNp on Apr 22, 2015 2:13:42 GMT -5
Ah, ok... for the use I have here I keep randomising every loop around on the title page to make it more 'random' ... random in the game for the location of sprites is definitely not the best.
cNp
|
|
|
Random
Apr 22, 2015 13:42:07 GMT -5
Post by mikiex on Apr 22, 2015 13:42:07 GMT -5
Thats quite a waste of cycles in someways, if you have cycles to spare you might as well do that though. A typical method is to have a counter that you keep adding 1 to, so every cycle the byte will be a different value. Then when the player presses the button to play the game you take that counter and put it into the seed for the random generator. In the case of the vectrex bios routine it uses 3 bytes (look at the docs for the location) Probably only putting your counter into one of the bytes will be good enough.
The hopefully when you start the game you will have different results each time you run it.
I've had a mess with some random number generators in 6809, but I've never plotted the distribution. I might have a go at that sometime to find a decent routine.
|
|
|
Random
Apr 23, 2015 5:40:02 GMT -5
Post by cNp on Apr 23, 2015 5:40:02 GMT -5
Yeah, I'm only doing it as I have cycles to burn on the title page but I like your idea of using some other value/counter to seed the random in game.
Would certainly be interested to see any output from tests you do on distribution.
cNp
|
|
|
Post by Kristof Tuts on Apr 25, 2015 8:14:16 GMT -5
Don't use the original bios random routine ! It has a bug that might crash your game (it occured during my vector pilot development).
Here is a patched version of the routine (bios labels are based on fred taft's disassembly):
get_random_bugfixed: ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;+This function is a bugfixed version of the original bios get_random_a function. ;+ ;+Inside the get_random_a and get_random_a2 routines of the vectrex BIOS. ;+A terrible bug is located: the X-pointer that needs to be pointing to ;+the random-seed in RAM is not loaded directly with the random-seed ;+address. Instead, it is loaded with the contents of the random-seed. ;+In some cases, this causes that new random-seed is written at random-places ;+in the memory map. Sometimes in a RAM-space, sometimes in a VIA-register. ;+This causes the program to crash ! ;+ ;+At entry: none ;+At exit : A = random-number. ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pshs b,x ;backup b,x clrb ;seed of 0 get_random_bugfixed_B_rounds: ldx #Random_Seed ;BUGFIX : set X-pointer to the ADDRESS-value of the random-seed ! jmp ran_loop ;continue with remaining ROM-routine (see exec-rom)
Regards,
Kristof
|
|
|
Random
Apr 25, 2015 8:43:26 GMT -5
Post by cNp on Apr 25, 2015 8:43:26 GMT -5
*bows down in the presence of Kristof*
;-D
Thanks so much for sharing that, really appreciated.
Whilst I am fine coding my game I pretty much know I'll never be doing anything really clever or looking too deeply to extract performance so it's great when the guys, like you and Christopher Tumber, share your investigations and solutions!
cNp
|
|
|
Post by mountaingoat on Aug 22, 2015 7:20:48 GMT -5
Lately I started using a very simple macro to generate random numbers with pretty good results. Does not quite have the distribution of a more complex random generator function but for 8 bit numbers it is acceptable and very simple and fast.
Have a variable in RAM for the seed, initialize it to some value (don't use 0 or 128) and just call the macro, it will put the random in register 'a' and update the seed for next time:
seed equ $C880
RAND_MACRO macro local rand_done lda seed asla bcc rand_done eora #$1d rand_done: sta seed endm
|
|
|
Random
Aug 28, 2015 17:10:19 GMT -5
Post by mikiex on Aug 28, 2015 17:10:19 GMT -5
Lately I started using a very simple macro to generate random numbers with pretty good results. Does not quite have the distribution of a more complex random generator function but for 8 bit numbers it is acceptable and very simple and fast. Have a variable in RAM for the seed, initialize it to some value (don't use 0 or 128) and just call the macro, it will put the random in register 'a' and update the seed for next time: seed equ $C880
RAND_MACRO macro local rand_done lda seed asla bcc rand_done eora #$1d rand_done: sta seed endm
-------------------- Thats very similar to what I was trying, a LFSR, I must get around to plotting some of these
|
|
|
Random
Aug 29, 2015 18:40:16 GMT -5
Post by mountaingoat on Aug 29, 2015 18:40:16 GMT -5
What I found is that as long as you generate a new seed (and ignore it) on some user input - say Joystick left - the random numbers are perceived as truly random by the player.
|
|