Functions and Random numbers

To gain experience using functions and learn how to generate random numbers.

We will write a game program that allows a person to guess a random number.

Guess My Number Game Enter a guess between 1 and 100 : 4 Too low! Enter a guess between 1 and 100 : 6 Too low! Enter a guess between 1 and 100 : 50 Too high! Enter a guess between 1 and 100 : 25 Too low! Enter a guess between 1 and 100 : 35 Too high! Enter a guess between 1 and 100 : 30 Too low! Enter a guess between 1 and 100 : 32 Too low! Enter a guess between 1 and 100 : 34 Too high! Enter a guess between 1 and 100 : 33 Correct! You got it in 9 guesses!

Review page on generating random numbers.

Questions:

1. How do you generate a number between 1 and 100 (inclusive)?

2. How do you generate a double number between n and m where 0 <= n < m?

Write and test functions:

voidinitRandom();intgetRandomInt(intmin,intmax);doublegetRandom();

Using the functions above write the number guessing game shown in the introduction.

Write a program that generates 100 numbers between 1 and 50. What is the average of these numbers? Can you compute the median of the numbers?

This topic: SCMP118 > Main > Spring2017 > HandsOnRandom

Topic revision: r2 - 2017-02-21 - JimSkon

Copyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.

Ideas, requests, problems regarding TWiki? Send feedback

Ideas, requests, problems regarding TWiki? Send feedback