How can I get a random number between x amount and x amount? Thanks.

BAGLe question...

Forum
» Making Games / Novice Questions and Answers
» BAGLe question...

BAGLe question...

Woad 04 Jun 2007 00:46

(Linkage, at school)

`number = rnd() % [big amount] + [small amoutt]`

This seems wrong… shouldn't it be

`r = rnd() % ( max - min ) + min`

?

E.g. i want a random number between 4 and 10 then

```
r = rnd() % ( 10 - 4 ) + 4
' Which becomes
r = rnd() % ( 6 ) + 4
' Where rnd() % 6 gives a number between 0 and 6.
' Add +4 to that and you have a random number between
' 4 and 10
```

Yeah, you're right.

If you want to generate a number between 4 and 10 you need to increase Max by 1…

`r = Rnd() % (11 - 4) + 4`

Which would become…

`r = Rnd() % (7) + 4`

^^

Don't worry about it, lol. :)

When I first started using Basic4GL I used to always have problems generating Random Numbers.

The Basic4GL Help Documentation isn't much help either.

If you want to generate a number between 4 and 10 you need to increase Max by 1…

of course. Modulus doesn't return the max itself :D … maybe someone should make this into a function and put it in the resources for b4gl. There should be a sources section :)

There is : source code. :) —hartnell

/forum/t-10998/bagle-question#post-