Jump to content
Sign in to follow this  
Gemintronic

Pseudo Random Number Generator in batari

Recommended Posts

Thinking about MausBoys "Holy Grail" of an Ultima clone for 2600 I realized level data would be a real concern. My thought would be to have a pseudo random number generator to provide the type of screen given a position in the world map. So, say, worldx = 10 worldy = 10 would always be screen type 6. Something like prand( worldx + worldy ) = 6 given a seed of 33 (or whatever).

 

I've used this in another language:

 

#define rand
var a, m, p_rand, range;
a = 16807;
m = 2147483647;
range = argument0;

if (range < 0) {
   global.p_rand_last = -range;
   return 0;
}

global.p_rand = (a * global.p_rand_last) mod m;
global.p_rand_last = global.p_rand;

return (global.p_rand / m) * range;

#define rand_ext
var a, m, p_rand, range;
a = 16807;
m = 2147483647;
range = argument0;
stream = argument1;

if (range < 0) {
   global.p_rand_ext_last[stream] = -range;
   return 0;
}

global.p_rand_ext = (a * global.p_rand_ext_last[stream]) mod m;
global.p_rand_ext_last[stream] = global.p_rand_ext;

return (global.p_rand_ext / m) * range;

 

Of course, much of the math doesn't carry over to Batari. I'm going to have to think small. I don't care about random distribution much. All I want is the same number given a value and a seed. I'd even put up with a hard-coded seed.

 

Any thoughts on how to proceed? What would such a Batari function would look like?

 

Could I get away with:

 

 function prand
rem add seed value
result = seed
rem add argument
result = result * temp1
return result
end

Edited by theloon

Share this post


Link to post
Share on other sites

All I want is the same number given a value and a seed.

I'm not sure I understand yet. The standard "rand" statement always returns the same value for a given seed. For example, "rand=1:a=rand" will always return 180, "rand=2:a=rand" will always return 1, "rand=3:a=rand" will always return 181, etc. Is that what you're looking for?

 

Michael

Share this post


Link to post
Share on other sites

All I want is the same number given a value and a seed.

I'm not sure I understand yet. The standard "rand" statement always returns the same value for a given seed. For example, "rand=1:a=rand" will always return 180, "rand=2:a=rand" will always return 1, "rand=3:a=rand" will always return 181, etc. Is that what you're looking for?

 

Michael

 

That would be it. I guess that's why people talk about seeding the rand function with a user joystick press. The randomness of human reflex makes the seed different for each game.

 

I didn't know if rand really worked that way. Thanks for clarifying! I suppose this could do:

 

 rand = worldx
whatscreen = rand
rand = worldy
whatscreen = whatscreen + rand

 

Of course, worldx = 10 worldy = 15 is gonna get the same whatscreen value as worldyx= 15 worldy = 10 thus the same room.. maybe?

Edited by theloon

Share this post


Link to post
Share on other sites

I suppose this could do:

 

 rand = worldx
whatscreen = rand
rand = worldy
whatscreen = whatscreen + rand

 

Of course, worldx = 10 worldy = 15 is gonna get the same whatscreen value as worldyx= 15 worldy = 10 thus the same room.. maybe?

That depends. Yes, "whatscreen" would be the same, but if you use a 16-bit number, the room could be different-- e.g., use the first "rand" value for the lo-byte, and the second "rand" value for the hi-byte, or vice versa.

 

But I don't understand what you need "rand" for. I mean, if you want the result of calling the routine to always be the same given particular x and y seed values, what do you need "rand" for-- can't you just use the seeds?

 

Michael

Share this post


Link to post
Share on other sites
can't you just use the seeds?

 

I suppose rand = worldx and rand = worldy would work but it'd generate the same room right?

 

Say if the rand result for a worldx seed is 15 and the rand result for worldy seed is 10 and I added them.. we get 25

now if the rand result for a worldx seed is 10 and the rand result for worldy seed is 15 and I added them.. we get 25

 

The result is both world screen (15, 10) and (10,15) are gonna be the same. Gotta be a way around that.

 

Thanks for sticking with me so far :)

Edited by theloon

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...