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!

More For Members

Lesson Plan

Coming Soon!

Presentation

Coming Soon!

Homework

Coming Soon!

Students

Coming Soon!

Not a member yet? Sign Up Here

Sign Up For Membership Today

Level Price  
Individual Teacher £3.99 per Month. After your initial payment, your first payment is Free. Select
Individual Student £2.50 per Month. After your initial payment, your first payment is Free. Select
Whole School £12.50 per Month. Select
iGCSE Computer Science Distance Learning (Feb Start) £21.00 now and then £21.00 per Month for 15 more Months. Select
iGCSE Computer Science Distance Learning (Sept Start) £21.00 now and then £21.00 per Month for 15 more Months. Select