Started by EgoTrip, 10:39, 16 December 10
0 Members and 1 Guest are viewing this topic.
Quote from: EgoTrip on 10:39, 16 December 10How do I generate random numbers in machine code? For example a number between 1 and 20?
Quote from: redbox on 10:56, 16 December 10There are some examples on the wiki. Generally you either use a simple formula based function (which is usually enough) or a look-up table.When I wanted some random numbers generated for the snow in my last demo I resorted to just using BASIC (using RND to generate the number and then POKEing it into memory) to generate a table. In the demo. this table was then offset on the screen when repeating which was enough to provide a 'random' effect.
result = result - ((result\range)*range)
final = lower_value + (result-((result\(upper_value-lower_value))*(upper_value-lower_value);
call rand8ld l,ald h,0ld bc,upper_value-lower_valuemod_loop:or asbc hl,bcjp p,mod_loopadd hl,bcld bc,lower_valueadd hl,bcld a,lret
LD L,RLD H,R
Quote from: phi2x on 12:36, 16 December 10And the bad news is that only the first value you get from R is "truely" random. If you get R values multiple times, the next value of R is the same as the one before, only incremented by some offset.
ld a,rld h,a.rand8ld b,aadd a,aadd a,aadd a,binc ald (rand8+1),ald l,a ; HL now contains a random 16-bit number
Quote from: redbox on 14:56, 16 December 10If you need a seed to make the number more random, you could also find the time since the CPC was turned on and use that?&BD0DKL TIME PLEASEActionReturns the time that has elapsed since the computer was switched on or reset (in 1/300ths of a second)EntryNo entry conditionsExitDEHL contains the four byte count of the time elapsed, and all other registers are preservedNotesD holds the most signifilcant byte of the time elapsed, and L holds the least significant; the four byte count overflows after approximately l66 days have elapsed.
var seed : byte; begin randomize; seed:=random(10); write(seed);end.
Quote from: Devilmarkus on 14:59, 16 December 10In BASIC you should use RANDOMIZE TIME
10 MODE 220 FOR a=1 TO 2330 RANDOMIZE TIME40 FOR b=1 TO 2550 PRINT USING("## ");INT(RND*100);60 NEXT b70 PRINT80 NEXT a
;; Return a number between 0 and 2 org &4000 ld a,(seed) ld b,a add a,a add a,a add a,b inc a ld (seed),a cp 0 jr c,skip_divide ;; Skip if Number equals 0.return rra cp 3 jr nc,return ;; Repeat if number 3 or greater.skip_divide ld (result),a ret.seed defb 0.result defb 0
;; Return a number between 0 and 2 org &4000 ld a,(seed) ld b,a add a,a add a,a add a,b inc a ld (seed),a cp 1 jr z,enter_result ;; Skip if Number equals 1.return rra cp 3 jr z,make_zero ;; If result is 3 make it 0 cp 4 jr nc,return ;; Repeat if number is 4 or more jr enter_result ;; Need to skip Make_Zero at this point.make_zero xor a.enter_result ld (result),a ret.seed defb 0.result defb 0
LD H,0LD L,ALD D,0LD E,AADD HL,HLADD HL,DE ; HL=3*randLD A,H ; HL is now between 0 and 2
if c>p: c'=(c>>1) XOR pif c<=p: c'=(c>>1)
if (c&1) c = (c^0x171) >>1; // note that the high bit is always 1else c = c>>1; // note that the high bit is always 0
rrca jr nc,noxor xor #38noxor:
org #800 ld hl,#8100 ; the buffer from #8100 contains the count of each rnd ld de,#8000 ; the buffer from #8000 contains the sequence of rnd ld b,0 ld a,1clear: ld (hl),e inc l djnz clear loop: ld (de),a inc de ld l,a inc (hl) rrca jr nc,noxor xor #enoxor: djnz loop ret
rand: ld a,#01 ; note we want to avoid 0 as a starting condition as that will always result in an output of 0 rrca jr nc,rand_noxor xor #erand_noxor: ld (rand+1),a ld h,0 ld d,0 ld l,a ld e,a add hl,hl add hl,de ld a,h ret
crc: xor #ff ; initial CRC value ld b,8crcloop: rrca jr nc,crc_noxor xor #ecrc_noxor: djnz crcloop ld (crc+1),a ret ; returns current CRC value
Quote from: ralferoo on 15:31, 20 March 14Code Select Expand ld h,0 ld d,0 ld l,a ld e,a add hl,hl add hl,de ld a,h Note that the multiply by 3 could be optimised further, but I'll leave that as an exercise to you!
ld h,0 ld d,0 ld l,a ld e,a add hl,hl add hl,de ld a,h
ld h,0ld l,aadd hl,hladd lld h,aadc 0
Quote from: ralferoo on 15:25, 20 March 14So, I thought I'd do a separate post about LFSR as it's complicated to explain. For some background reading, see Linear feedback shift register - Wikipedia, the free
Page created in 0.326 seconds with 27 queries.