Welcome to "Learn by Code"!

Welcome back fellas
This is Part 2 of our amazing coding series where we learn, code, and understand new concept together in a particular programming language.

Here we dive deep into the difference between bad habits and good habits, and explore new concepts that make you a smarter, faster, and stronger developer.

In This part we’re going to build some basic projects using RNG.

So, let’s not waste another second thinking about it,

What is RNG

Before making a game with the help of RNG we need to understand what is RNG.

RNG stands for Random Number Generator.

RNG is a algorithm which produces a single or sequence of numbers that cannot be reasonably predicted as they are random.

In Simple words,

It just a program that generates random numbers for us which cannot be reasonably predicted

Where else we see RNG

  • Loot drops in games
  • Procedural generation
  • Rogue like games
  • While Rolling Dice
  • Coin Toss

There are 2 types of RNG

  • True RNG - Pure Randomness.
  • Pseudo RNG - Faked Randomness using algorithm that look random but are actually predictable.

Most popular games today, like Minecraft and Balatro, appear to be using True RNG...
BUT! They actually use Pseudo RNG

RNG in Mini Micro

Now that we understand what RNG is, let's discuss how to implement it in Mini Micro.

In Mini Micro, you don’t need to manually write a full RNG (Random Number Generator) algorithm. Instead, Mini Micro provides a built-in command that makes generating random numbers easy: the rnd.

Basic Usage of rnd

If we use rnd directly like this,

toss = rnd
print "You tossed " + toss

It will output a random number between 0 and 1.

However, it is important to note:

  • The generated number can include 0 but will never exactly be 1.
  • The result will typically have up to 6 decimal places.

Controlling the Output

You might have noticed that the random numbers have a lot of decimal points.
If you want a clean, whole number instead, you can combine rndwith rounding functions like ceilor floor:

  • ciel() - Rounds UP to the nearest whole number
  • floor()- Rounds Down to the nearest whole number

Let's Create a Coin Toss Game

In programming, it's considered good practice to first write pseudo-code for the program you want to build before making actual code.

For beginners, this is very helpful, as it helps you understand your own code more precisely and also boosts productivity. Even for experienced developers creating complex programs, pseudo-code can help as a solid foundation.

What is Pseudo-Code?

Pseudo-code is simply a way to describe your program using basic flowcharts or English-like statements, without worrying about the syntax of a specific programming language.

The Pseudo-Code for Coin Tossing

Before we start coding, let's write down the pseudo-code for our coin toss program:

//THIS IS A BASIC COIN TOSSING PROGRAM
//USED FOR LEARN BY CODE 1.2 BY SELFISH DEV
//GENERATE A RANDOM NO.
//SAVE THAT AS A VAR
//ROUND DOWN THAT VAR
//IF THE VALUE OF VAR IS 1 THEN PRINT HEADS
//ELSE PRINT TAILS

Now that we have our pseudo-code, let’s begin coding. While writing the program, try to follow the steps outlined in the pseudo-code and compare it to your own approach.

Remember: Practice makes perfect!

The Bad Version of Coin Toss Code

Before understanding the good code, let's first have a look at this bad version of this coin toss program:

toss = rnd*2
if toss = 1 then
    print "Heads"
else
    print "Tails"

This code has several issues, such as:

  • Lack of clarity
  • Error
  • Not user-friendly
  • Common Mistakes

Take a look and try to spot the mistakes. If you find the most mistakes, you’ll get a shout-out in the next post! 😄

The Good Version of Coin Toss Code

Here’s the better version of the coin toss game:

// Generates a random number between 0 and 1, then multiplies by 2
// The result is rounded down to either 0 or 1 (representing heads or tails)
toss = floor(rnd * 2)

// Check the value of toss to decide heads or tails
if toss == 1 then
    print "You just tossed HEADS"
else
    print "You just tossed TAILS"
end if

In this version we have:

  • The code is clearer.
  • It’s easier to understand.
  • It’s more user-friendly
  • Less error prone

But can we take it even further?

PRESENTING THE ULTIMATE COIN TOSS PROGRAM

Here’s a more engaging and user-friendly version of the coin toss game:

print "Welcome to coin toss."
user = input("Enter TOSS to toss the coin or QUIT to exit: ").upper

if user == "TOSS" then
    choice = input("Heads or Tails: ").upper
    if choice != "HEADS" and choice != "TAILS" then
        print "ERROR-ENTER VALID FACE"
    end if
else if user == "QUIT" then 
    exit
else 
    print "Enter a valid command"
    exit
end if

toss = (rnd * 2)
if toss >= 0.5 then
    print "You just tossed HEADS"
    if choice == "HEADS" then
        print "You WON"
    else if choice == "TAILS" then
        print "You LOST"
    end if
else
    print "You just tossed TAILS"
    if choice == "HEADS" then
        print "You LOST"
    else if choice == "TAILS" then
        print "You WON"
    end if
end if

Now lets understand why the code I wrote now is 10x better than what I made previously.

USER FRIENDLY
-It asks the user for input before the toss happens. The player chooses "Heads" or "Tails", and based on their input, the game gives feedback on whether they won or lost.

-If the user inputs something other than "Heads" or "Tails", it prompts an error, ensuring only valid input is accepted.

-It allows the user to exit the game gracefully if they type "QUIT".

-It tells the user if they won or lost, which creates a better user experience.

-The upper method is applied to the user input (for both commands and choices), ensuring that regardless of whether the user enters "heads", "Heads", or "HEADS", the input is converted to uppercase for consistency.

Complexity and Fun Factor

-It turns the coin toss into a fun guessing game where the user actively participates, making the experience more engaging and interactive.

-It simulates a betting/guessing game where the user must predict the outcome.

-It allows for the possibility of winning or losing, introducing a basic form of game progression.

Error Handling and User Experience

-Handles incorrect input.

-Provides guidance like "Enter TOSS to toss the coin or QUIT to exit", which helps the user understand what to do next.

AND THERE ARE MANY MORE THINGS THAT MAKE THIS VERSION OF CODE BETTER

CHALLENGE

Now that you understand the basics of RNG and how to create a simple coin toss game, I challenge you to create your own RNG Dice Game!

Drop your best Dice Game code in the comment section, and I’ll review the top submission. Best code will get a shout-out in the next post! (Unfortunately, no prizes yet, but hey, the appreciation is worth it 😄).

Good luck, and may the best coder win!

Social Links

Miniscript Site

Miniscript Wiki for rnd

Minimicro Site

My Youtube

My Itch Page