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.

GCSE computer science

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?

Find this page helpful? Share the love on your social media mentioning @TeachAllAboutIT and we’ll enter you in our monthly draw to win a gift voucher for any product on the site!

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on reddit

More For Members

Lesson Plan

Coming Soon!


Coming Soon!


Coming Soon!


Coming Soon!

Not a member yet? Sign Up Here