Jump to content
IGNORED

IntyBASIC Seeding of random number generator


Recommended Posts

I'm wondering if there's an easy way to seed the random number generator in IntyBASIC? My goal is to have the "terrain" in a game always have the same pattern, but not to have to use a large table of numbers to create it.

 

I know I could make my own "random" number generator, or I could read ROM values, but maybe there's an easier way, such as a simple poke to a RAM address?

Link to comment
Share on other sites

You could seed IntyBASIC’s PRNG with a POKE.  You can find the address by looking in the listing file generated by the assembler.
 

However, because the EXEC clears all memory during initialization, and the machine’s bootstrap sequence is deterministic, you are guaranteed to start always with the same seed.

 

As a matter of fact, about the only way to add entropy to the IntyBASIC PRNG is to advance it during idle times and let the player’s reaction add non-deterministic delay.

 

So, if you want a deterministic pseudo-random sequence that is always the same, just make sure to initialize your terrain before you enable any user interaction.

 

    dZ.

Link to comment
Share on other sites

Thanks. Maybe it's easiest if I make my own generator. Then I'll know for sure the sequence. It's not feasible to load all the numbers at boot-up, and I'll want the same sequence even for repeated plays (at least in the "fixed" gameplay mode).

Link to comment
Share on other sites

Somewhere in this forum, there is a library I posted for a PRNG using a Galois LSFR.  It is in assembly language, though, although it includes an IntyBASIC interface.

 

The main routine is rather compact, just a shifts and XORs.

 

If interested, I could port it to IntyBASIC proper, although it may not be as fast.

 

    dZ.

  • Like 1
Link to comment
Share on other sites

Because that takes more work and space?

 

Non-deterministic:

- read location $9FFE.

 

Deterministic:

(from Developer's Guide)

 

● Set   up   a   “seed”   in   memory   to   denote   the   game’s   notion   of   “current   random   number”. 


● To   generate   the   next   random   number,   perform   the   following   steps: 
a. Copy   the   seed   from   RAM   to   location    $9FFD 
b. Write   a   fixed   value   to    $9FFC 
c. Copy   the   updated   seed   from    $9FFD    back   to   RAM. 


● Use   the   updated   seed   as   the   next   random   number.      

 

 

 

  • Like 1
Link to comment
Share on other sites

I think it also depends on whether the requirement is to generate a single level terrain (which could be done during the game’s initialization), or various throughout the program’s lifecycle.

 

If it is a single one, then I say avoid all the complexity, let the EXEC and IntyBASIC runtime seed the PRNG normally (which is deterministic), and generate the sequence prior to enabling user interaction (right after bootstrapping, before any idle loops waiting for user input).

 

If you need a repeatable means of generating various sequences each based on their own seed, then the simplest solution appears to be to use JLP, like @cmadruga suggested.

 

I have never used JLP for this, so I do not know what overhead is needed in order to call the API.  However, I thought that enabling JLP acceleration and enhanced functions required the allocation of a chunk of System and Cartridge RAM for the code and data needed to interface between the platforms.  My understanding was that it did not come for free.  *shrug*
 

    dZ.

Link to comment
Share on other sites

In my case, I'm not going to use JLP features, because I don't have JLP boards. There may be other advantages to moving to JLP, but if it's just for the random numbers, it's not worth it.

 

For my current requirement (but feel free to discuss other requirements), I need to determine the location of an obstacle every second, approximately. I could compare it fairly closely to something like Activision Skiing for the Atari 2600. I want an option for completely random obstacle locations, but for equal comparison of high scores, I also want an option for fixed obstacle locations (fixed, as in the same each time the game is played). The length of the "course" is variable, and there will be several of them, so a fixed table is less viable (or more complex than a pseudo random number generator). I haven't tried it yet, but for the fixed obstacle locations, I think a simple bit shift and XOR system (as I linked above) will be acceptable. Otherwise I'll go with something slightly more complicated, such as described here: https://en.wikipedia.org/wiki/Linear-feedback_shift_register.

Link to comment
Share on other sites

3 minutes ago, carlsson said:

So far we have 2 ready made solutions + JLP if you use it, beyond what the language already implements. Can't you read the assembly code and figure out how hijack it with a seed?

 

It isn't clear to me that those 2 solutions wouldn't require special boards or dipping below BASIC into assembly. 

Link to comment
Share on other sites

26 minutes ago, carlsson said:

So far we have 2 ready made solutions + JLP if you use it, beyond what the language already implements. Can't you read the assembly code and figure out how hijack it with a seed?

23 minutes ago, Gemintronic said:

 

It isn't clear to me that those 2 solutions wouldn't require special boards or dipping below BASIC into assembly. 

I'm not sure exactly what the "2 ready made solutions" are, but definitely there are a number of easy solutions available. Probably the easiest for me, for this non-critical task, is to make my own generator using a few lines of BASIC code. Yes, a quick "poke" (in BASIC, with no assembly required) could likely work - just need to know the address, and know where to find it next time. For me, I'll be more comfortable making my own generator in cases where I need an input seed (or use an assembly-generated one, but of course not in cases such as contests where that's not allowed).

Edit: yes, JLP requires a special PCB (or can be enabled in jzintv for emulator use).

  • Like 2
Link to comment
Share on other sites

1 hour ago, Gemintronic said:

A PRNG that you can seed and get the same results every time is pretty important.  For me it's the easiest way to fit huge levels without gobbling up ROM.

 

Maybe I'll find a BASIC coded PRNG and convert it into IntyBASIC.


I provided above a ready-made assembly language library that does it for you.

 

Deterministic vs. non-deterministic is just a matter of adding entropy or not.  Typically, you advance the PRNG during idle times, or corrupt it with some user input in order to add entropy.  However, if you only advance it when you retrieve a number, then it will always follow the same sequence when starting from the same seed.

 

I believe the IntyBASIC runtime advances the PRNG during idle loops of the WAIT statement which, depending on your program, could be non-deterministic if they are influenced by user input (i.e., if you have different code execution paths depending on user input).  So that may not be ideal.

 

    dZ.

  • Like 1
Link to comment
Share on other sites

54 minutes ago, Gemintronic said:

 

It isn't clear to me that those 2 solutions wouldn't require special boards or dipping below BASIC into assembly. 


My PRNG library above does not require any special hardware.  It is in Assembly Language, true, but it includes a handy interface for IntyBASIC, and simple to follow instructions on how to integrate it into your program.

 

    dZ.

  • Thanks 1
Link to comment
Share on other sites

2 minutes ago, DZ-Jay said:


My PRNG library above does not require any special hardware.  It is in Assembly Language, true, but it includes a handy interface for IntyBASIC, and simple to follow instructions on how to integrate it into your program.

 

    dZ.

 

Sorry to ask.  I re-read the topic and still feel like I'm guessing at the solution you made.  Is there a link to the PRNG and how to use it in IntyBASIC?  Thank you in advance!

Link to comment
Share on other sites

FYI, I used the method as described in the link in post #4 above, and it works well for this application.

I did have to do adjust the seed and XOR values for each level, to get the patterns to my liking, but some solution or other would be required no matter which generator I had used.

  • Like 1
Link to comment
Share on other sites

8 hours ago, 5-11under said:

FYI, I used the method as described in the link in post #4 above, and it works well for this application.

I did have to do adjust the seed and XOR values for each level, to get the patterns to my liking, but some solution or other would be required no matter which generator I had used.

 

I supposed you implemented it in Assembly Language, right?  I can't see any other way to rotate bits in IntyBASIC.

 

     -dZ.

Link to comment
Share on other sites

2 minutes ago, DZ-Jay said:

 

I supposed you implemented it in Assembly Language, right?  I can't see any other way to rotate bits in IntyBASIC.

 

     -dZ.

I used IntyBASIC. I just used a 16 but number, so I could catch the carry bit.

 

In this case, the game is for the current contest, so no assembly allowed. Otherwise I probably would have used your code.

  • Like 1
Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

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...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...