Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:17 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : RND, RANDOMIZE - suggestion for 5.05.05

Author Message
electrotrains
Newbie

Joined: 29/07/2020
Location: United Kingdom
Posts: 13
Posted: 08:13pm 02 Aug 2020
Copy link to clipboard 
Print this post

Hi all,

I'm in the early stages of writing a Terraria-style game on the CMM2. (If you don't know Terraria - if you think of Minecraft in 2D running on a 16-bit era console, you'll be close-ish!).

I was explaining roughly to my son the concept of seeding random numbers, and how we could use that to generate random, but repeatable, worlds.

Sadly, when looking up RND() in the manual, I discovered:
The Colour Maximite 2 uses the hardware random number generator in the STM32 series of chips to deliver true random numbers. This means that the RANDOMIZE command is no longer needed and is not supported.


EDIT - I've just noticed there is a listing for a pseduo random nubmer generator written in basic in the "Porting Programs" section of the manual.

Is there any chance we can have this added back to the language itself? I imagine that a version written in BASIC will almost certainly be many times slower than a native implementation.

Maybe:
RANDOMIZE seed
and RAND (rather than RND) to get the next in the series.

Robin
https://github.com/robinhedwards
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 09:09pm 02 Aug 2020
Copy link to clipboard 
Print this post

Not possible  - no more command slots just use the Basic version
 
Sasquatch

Guru

Joined: 08/05/2020
Location: United States
Posts: 377
Posted: 04:16am 05 Aug 2020
Copy link to clipboard 
Print this post

Partly due to my own curiosity and partly due to several requests here on TBS, I decided to play with LFSR pseudo-random number generators.

The following function implements a 16-Bit LFSR "random" number generator as used on the 8-bit computers of the '80's  It will generate a repeating sequence of 65535 numbers with a fairly good random distribution.

The sample program will plot the distribution of the numbers.  If you watch carefully, you will see the plot "flat-line" every 65536 calls and the sequence then repeats.

Warning: This function does not produce truly random numbers!  Do not use for any kind of security purposes.  The algorithm is simple and common.  The sequence can be easily determined!  The Rnd() function built in to MMBasic produces better random numbers and does it 15X faster.  This function is for nostalgic purposes and those rare cases when a repeatable sequence of numbers may be needed.

I chose to use the syntax from the C64 for the parameter:

'16 Bit Galois LFSR Pseudo Random Number Generator
'Returns pseudo random numbers in range of 0(inclusive) to 1.0(exclusive)
'Par = <positive number>  Return next number in sequence
'Par = <0>  Seed from system Timer
'Par = <negative number> use Par as the seed
'Note:Calling Rand(0) repeatedly will not give a good distribution -
'  due to low resolution of the system timer

'Random number distribution Histogram

Mode 1,8

C = RGB(white)
DIM A%(800)

CLS

'R = Rand(-1)'Seed the LFSR with "1"
R = Rand(0)  'Seed the LFSR from the system Timer

Do
 R = 800 * Rand(1)  'Get the next number in the sequence
 A%(R) = A%(R) + 1
 Pixel R,600 - A%(R),C
Loop While A%(R) <= 600



'16 Bit Galois LFSR Pseudo Random Number Generator
'Returns pseudo random numbers in range of 0(inclusive) to 1.0(exclusive)
'Par = <positive number>  Return next number in sequence
'Par = <0>  Seed from system Timer
'Par = <negative number> use Par as the seed
'Note:Calling Rand(0) repeatedly will not give a good distribution -
'  due to low resolution of the system timer
Function Rand(Par)
 STATIC LFSR% = &hAAAA
 LOCAL LSB%

 If Par = 0 Then
   LFSR% = Timer Mod &hFFFF
 ElseIf Par < 0 Then
   LFSR% = ABS(Par) Mod &hFFFF
 EndIf
 
 LSB% = LFSR% AND 1
 LFSR% = LFSR% >> 1
 If LSB% Then LFSR% = LFSR% XOR &hB400  
 Rand = (LFSR% - 1) / &hFFFF
End Function

-Carl
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025