Code: Select all

```
u16 randomNumberGenerator(u16 finishInt) {
u16 x = 0;
do {
x = random();
} while (x <= 0 || x > finishInt);
return x - 1;
}
```

u16 randomNumberGenerator(10) would give me random 1 to 10

**Moderator:** Stef

Code: Select all

```
u16 randomNumberGenerator(u16 finishInt) {
u16 x = 0;
do {
x = random();
} while (x <= 0 || x > finishInt);
return x - 1;
}
```

u16 randomNumberGenerator(10) would give me random 1 to 10

No, that's really wasteful, and can run infinite time. Hint: modulo operator.

Code: Select all

```
x = (random()%finishInt-1)+1;
```

P.S you've just helped me no end! I'm really grateful

I would add a couple parenthesis (), otherwise the calculation order may not be what you expect.