# Difference: HandsOnRandom (1 vs. 2)

#### Revision 22017-02-21 - JimSkon

Line: 1 to 1

 META TOPICPARENT name="Spring2017"

# Hands On

Line: 44 to 45
%CODE{"c++"}% void initRandom(); int getRandomInt(int min,int max);
Changed:
<
<
double getRandomInt(double min,double max);
>
>
double getRandom();
%ENDCODE%

### Step 2

#### Revision 12017-02-21 - JimSkon

Line: 1 to 1
>
>
 META TOPICPARENT name="Spring2017"

# Hands On

Functions and Random numbers

### Goal

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!```

### Step 1

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:

`<-- SyntaxHighlightingPlugin -->`
```void initRandom();
int getRandomInt(int min,int max);
double getRandomInt(double min,double max);```
`<-- end SyntaxHighlightingPlugin -->`

### Step 2

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

### Step 3

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?

Copyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback