# Library Routines: ROUND and RANDOM

- **Track:** Cambridge IGCSE™ Computer Science (0478 / 0984) (CAMBRIDGE IGCSE 0478)
- **Module:** Section 8: Programming Concepts & File Handling
- **Estimated Duration:** 10 minutes
- **Access:** Open Theory (All-Access for Interactive IDE &amp; Autotests)

---

## Theory & Conceptual Background

A library routine performs a predefined operation. Use its documented parameters and returned value instead of rebuilding it.

## Rounding a value or expression

`ROUND(value, places)` returns a value rounded to the requested number of decimal places. The first argument can be an identifier or expression; places is an integer. For example, `ROUND(14.0 / 3, 2)` gives 4.67. `ROUND(4.67, 0)` gives 5. Rounding is different from discarding fractional digits.

## Random values

`RANDOM()` takes no arguments. Cambridge specifies a real result in the range 0 to 1 inclusive. Do not predict a particular next value. If R is in that range, `R * 10` lies between 0 and 10, and `ROUND(R * 10, 0)` is a whole number between 0 and 10. Rounded endpoints are not equally likely: do not describe this as a fair die.

```pseudocode
DECLARE Reading : REAL
Reading <- RANDOM()
OUTPUT ROUND(Reading * 10, 0)
```

## Worked example: cost per item

A batch costs 17.0 and contains 6 items. Divide first: 17.0 / 6. Round the result to two decimal places: 2.83. Do not round the total before dividing if that loses necessary precision.

## Independent application

Predict the minimum and maximum possible result of `ROUND(RANDOM() * 4 + 2, 0)`. Explain why running the program once cannot establish that the range is correct.

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Application: Routines and ranges (Fill in blank, 30 XP)

**Instructions:**
Work out each answer before submitting. ROUND(14.0 / 3, 2) returns: Minimum result of ROUND(RANDOM() * 4 + 2, 0): Maximum result of the same expression:


**Starter Code:**
```text
{"prompt":"Application: Routines and ranges","template":"1. ROUND(14.0 / 3, 2) returns: [blank_1]\n2. Minimum result of ROUND(RANDOM() * 4 + 2, 0): [blank_2]\n3. Maximum result of the same expression: [blank_3]","blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}],"word_bank":["4.67","4.66","2","3","6","7","5"]}
```


### Exercise 2: Independent Practice: Unit cost (Pseudocode, 30 XP)

**Instructions:**
Read a real batch cost followed by a positive integer item count. Output the cost per item rounded to two decimal places. A whole-number result may be printed without trailing zeroes.

Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.


**Starter Code:**
```text
// Plan your solution, then write your program.

```

**Autotest Expectations:**
- **Recurring decimal**: `test_case` -> Expected: `{&quot;inputs&quot;:[17.0,6],&quot;output&quot;:&quot;2.83&quot;}`
- **Already exact**: `test_case` -> Expected: `{&quot;inputs&quot;:[12.0,4],&quot;output&quot;:&quot;3&quot;}`
- **Round upwards**: `test_case` -> Expected: `{&quot;inputs&quot;:[10.0,6],&quot;output&quot;:&quot;1.67&quot;}`

