Mario Paint/random number generator
Talk0
83pages on
this wiki
this wiki
< Mario Paint
Mario Paint has a random number generator. When you power on or reset the game, Mario Paint uses the entropy from uninitialized RAM to seed the generator.
To find the random number generator in Mario Paint (JU), see addresses $000448, $00044a, $00044c, $008555, $01e20c, $01e238, $01e24b in the Mario Paint/address map.
Applications
Edit
- The title screen uses the generator to control Mario.
- The letter "P" of the title screen uses the generator to place the Fire Flowers, to control the dog and to control the flying saucer.
- Undodog uses the generator to dance.
- Gnat Attack uses the generator to move the bugs. Some bugs (including the boss) use the generator to decide when and how to attack.
Ruby port
Edit
Here is a port of the generator from Mario Paint to Ruby. The purpose of this port is to study how Mario Paint generates random numbers.
- FIXME, this Ruby port yields some but not all of the same numbers that Mario Paint would yield.
@state = [0] * 55
@upper_bound = 0x0100
def random()
@state_index += 1
if @state_index >= 55
stir_random
@state_index = 0
"value in RAM $000542"
else
@state[@state_index]
end
end
def seed_random(seed)
n1 = 1
n2 = seed & 0x001f
@state_index = 0
i = 20
54.downto(1) do |j|
@state[i] = n1
n1 = (n2 - n1) % @upper_bound
n2 = @state[i]
i = (i + 21) % 55
end
3.times { stir_random }
nil
end
def stir_random()
0.upto(54) do |i|
j = (i + 31) % 55
@state[i] = (@state[i] - @state[j]) % @upper_bound
end
nil
end