Question

so im pretty new to text based programming languages. with only one i am fluent with. (BATCH) i have coded some simple things before in BASIC and the X16 caught my eye because im really interested in C64 things. even though i dont have one of my own. so, jumping straight in, certain things i found that worked on the C64 dont work in the X16. and some things that im not even sure are possible even on a C64. so, as the title says, i have some simple questions.

how can i make a random number generator capable of outputting 2 different 1 digit numbers?

how does one program in assembly? no, im not asking to learn assembly. im asking how i actually start writing assembly code.

Recommended Posts

• 0

Check out the links provided they really do help.

RND takes one of three parameters.   RND(1) [or any positive parameter, generally] generates a random number between  0 and 1 based on the next 'seed' -- an internal number that is behind the maths of this pseudo-random number generator) -- and updates the seed.   RND(0) does the same thing but uses a byte of the internal clock as the seed.    RND with a negative number [i.e., RND(-2.39997)] uses the supplied parameter as, in essence, the seed.    Which means calling it with the same neg. number gives the same random number between 0 and 1.

So   A=RND(1) is the basic function.     But to get what you want, you have to do something with that random decimal value it spits out, which again is generated as a number greater than 0 and less than 1.

If you want an integer (no decimal fraction) number from 0 to 9 then reason it out:    You need to us the INT() function too.     And you take that decimal number between 0 and 1 and multiply it by the highest number you want + 1 (assuming you want the entire range of 0 to the highest number)

R=INT(RND(1)*10)

will cause the variable 'R' to have a random number from 0 to 9.     (Think about it, even if the RND() function itself returns .99999999 multiplying it by 10  gets you 9.999999 and then taking the INT() function of that returns a 9.

About the INT() function.   Its not 'rounding' in the sense you might be used to, i.e., in the event the fractional component is greater than .5 it does not 'round up'

Instead, INT() is a 'floor' style rounding, which means the output is always LESS than the input.     With a positive number, say INT(9.9999), it returns the next integer that is LESS than the value supplied, which is to say it disregards the decimal fraction, i.e., it returns 9 in this example.

But with a negative number, say  INT(-10.999), it ALSO returns the next integer that is LESS than the value supplied, i.e.,   -11 here.

All of that is why  INT (RND(1)*10) gives you a value between 0 and 9

So, if you want it to return two random single digit numbers, let's say you want to put them in variables to use later you need two calls of this function.

10  N1=INT(RND(1)*10)

20  N2=INT(RND(1)*10)

30  PRINT N1, N2

EDIT: Just read more carefully and saw your thing about wanting either a 1 or 2...   that means you multiply by the DIFFERENCE between the lowest number and highest number you want +1.

To have a lower boundary you just add it as the base to your calculations, i.e,

N1 = 1+INT(RND(1)*2)

N2 = 1+INT(RND(1)*2)

To test these, try this FOR/NEXT loop from the command line:

FOR L=1 TO 10: PRINT 1+INT(RND(1)*2):NEXT

After it prints its sequence of 1s and 2s, scroll your cursor up and hit enter on the same command again and watch the sequence change.  Viola.  Pseudo randoms of either 1 or 2.

EDIT:   Geez, I was pretty sloppy in answering this...  so I went in and fixed some typos and added clarity.   Substance is the same, it just reads better.

Edited by Snickers11001001
• 1
Share on other sites

• 0
49 minutes ago, RingHogger said:

how can i make a random number generator capable of outputting 2 different 1 digit numbers?

how does one program in assembly? no, im not asking to learn assembly. im asking how i actually start writing assembly code.

Start by writing a basic program that prints a single digit number to the screen. Then expand upon it for a second digit. The RND() function with examples can be found at https://www.c64-wiki.com/wiki/RND ( the C64 & X16 run V2 Basic)

You'll need to either use the online emulator or get it installed on your machine. See https://www.commanderx16.com/forum/index.php?/files/category/1-official-software/

For assembly, you can enter the monitor and start entering simple/short sequences by hand and executing them.  See https://www.c64-wiki.com/wiki/Final_Cartridge_3#Monitor_Program for very terse instructions and realize that you'll quickly outgrow it as a programming platform, rather use it to debug/troubleshoot.

Once you've had enough "hand coding", there is a package for programming on the X-16 here - https://sites.google.com/view/x16asmenv/home I believe the plan is for this to be in the final ROM for the X16.

After that, there are several cross compilers available for different OSes. Some require you to compile them to use, others include binaries to run. You'll need to be able to transfer the compiled programs between the host and the emulator, which has its own learning curve.

Feel free to continue asking questions as you go, and even post along the way on your experience into X-16 programming.

Edited by Edmond D
minor typos fixed :-)
Share on other sites

• 0
57 minutes ago, Edmond D said:

Start by writing a basic program that prints a single digit number to the screen.

well, i wrote one. but it only prints decimals. heres the code. im trying to make one that generates either a 1 or a 2.

1 cls

2 print RND(2)

3 input z

4 goto 1

Edited by RingHogger
adding something i didnt mention before
Share on other sites

• 0
32 minutes ago, Snickers11001001 said:

RND takes one of three parameters.   RND(1) [or any positive parameter generally] generates a random number between  0 and 1 based on the next 'seed' -- an internal number that is behind the maths of this pseud-random number generator) -- and updates the seed.   RND(0) does the same thing but uses the internal clock as the seed.    RND with a negative number [i.e., RND(-2.39997)] uses the supplied parameter as, in essence, the seed.    Which means calling it with the same neg. number gives the same random number between 0 and 1.

So   A=RND(1)  is the basic function.     But to get what you want, you have to do something with that random decimal value greater than 0 and less than 1.

If you want an integer (no decimal fraction) number from 0 to 9 then reason it out:    You need to us the INT() function too.     And you take that decimal number between 0 and 1 and multiply it by the highest number you want + 1 (assuming you want the entire range of 0 to the highest number)

R=INT(RND(1)*10)

will cause the variable 'R' to have a random number from 0 to 9.     (Think about it, even if the RND() function itself returns .99999999 multiplying it by 10  gets you 9.999999 and then taking the INT() function returns a 9.

About the INT() function.   Its not 'rounding' in the sense you might be used to, i.e., if the fractional component is greater than .5 it does not 'round up'

Instead, INT() its a floor style rounding, which means the output is always LESS than the input.     With a positive number, say INT(9.9999) returns the next integer that is LESS than the value supplied, which is to say it disregards the decimal fraction, i.e., it returns 9 in this example.

But with a negative number say  INT(-10.999) it ALSO returns the next integer that is LESS than the value supplied, i.e.,   -11 here.

All of that is why    RND(1)*10 gives you a value between 0 and 9

So if you want it to return two random single digit numbers, let's say you want to put them in variables to use later you need two calls of this function.

10   N1=INT(RND(1)*10)

20  N2 =INT(RND(1)*10)

30 PRINT N1, N2

thanks a bunch. this was very helpful. Share on other sites

• 0

Snickers11001001 has provided you with an excellent tutorial of the RND function. I'd suggest that you skim through Commodore's manuals on Basic (both the Vic-20 and the C64) just to round out your knowledge of V2 Basic. They are simple but cover a ton of useful information with an appropriate sequence for rapid learning.  Start with the user's guides, then move onto the programmer's guides. There are also several magazines from that period that have well written lessons too. See https://www.commodore.ca/commodore-manuals/ for a list of some, goolge for other sources.

• 1
• 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.