Random Numbers

As a programmer, you may find that your program needs to generate a number or letter in a range. This requires the program to create a random number. Games are an excellent example of this, with the use of dice and elements of chance.

Gif showing the steps of bubble sort for numbers

Consider generating a dice rolling program.

What is the lowest number of your dice? What is the highest?

What will you do with your random number once it is generated? Often this requires a following IF statement to make use of the random number.

But random numbers are not just used for rolling dice and games of chance. When sending data across a network, a wireless router waits a random amount of time before transmitting the data to avoid collisions. This means that when two computers are communicating, they are able to use two way communicatio, sending and receiving data along the same channels.

Random numbers are also often used to generate secure passwords that are difficult to crack. Imagine that you need to generate a new user password that matches all of the requirements for a strong password. Random numbers would be perfect for this job:

password = ""

FOR i = 1 TO 6 DO

     password = password + Chr(Random(97,122)) // adds a lower case

    password = password + Chr(Random(65,90)) // adds an upper case

    password = password + Str(Random(0,9))


    password = password + Chr(Random(35,46)) // adds a symbol


    password = password + Str(Random(0,9))

    password = password + Chr(Random(35,46)) // adds a symbol

OUTPUT password

 

This is one simplistic way to solve this problem. Can you think of a more efficient solution?

More For Members

Lesson Plan

Coming Soon!

Presentation

Coming Soon!

Homework

Coming Soon!

Not a member yet? Sign Up Here