To generate a 'random' number, you have a few options (naturally). For example, you can use the *r* register which will hold some value:

```
ld a,r ;a is the only register with which you can do this
ld h,a
ld a,r
ld l,a
;HL now has a random-ish value. Let's be crazy.
ld e,(hl)
ld a,r
ld h,a
ld d,(hl)
;Now D and E are loaded from unknown areas in memory
ld a,r
ld h,a
ld a,r
ld l,a
add hl,de
ret
```

Admittedly, that is rather complicated, but randomness needs complexity! If you want something to generate numbers that can seem randomish, but that you can seed, you can do something like this:

```
PseudoRandWord:
;Outputs:
; BC was the previous pseudorandom number
; HL is the pseudorandom number
;f(n+1)=(241f(n)+257) mod 65536 ;65536
;181 cycles, add 17 if called
ld hl,(randSeed)
ld c,l
ld b,h
add hl,hl
add hl,bc
add hl,hl
add hl,bc
add hl,hl
add hl,bc
add hl,hl
add hl,hl
add hl,hl
add hl,hl
add hl,bc
inc h
inc hl
ld (randSeed),hl
ret
```

I used that routine for a program I made (never finished) that needed a deck shuffling routine. It only repeats a number once every 65536 times, so it is pretty predictable in that sense, but think of this: If you want to generate 52 numbers from this, you are guaranteed no repeats, so they can arranged in ascending and descending order. Also, if you want to save a replay of the game, you can just save the random seed and record key presses to ensure that the deck will shuffle correctly when replayed.

As to time, the calculator saves time as a 32-bit number in ports 45h,46h,47h,48h in little endian order. For example, you can do this to get the time (mod 65536) :

```
in a,(45h)
ld l,a
in a,(46h)
ld h,a
bcall(_DispHL)
ret
```

However, note that this only works on the TI-84+ calculators since the TI-83+ models do not have a clock.