IGNORED

# Random number between 1 and 28?

## Recommended Posts

I need to change my web site over to the new look and work on my version of the bB help page some more, but I need to try something with bB before I forget about it. For this thing I'm trying, I require a random number between 1 and 28. That's easy with other BASIC-type languages, but what is the best way with bB?

Thanks.

##### Share on other sites

I need to change my web site over to the new look and work on my version of the bB help page some more, but I need to try something with bB before I forget about it. For this thing I'm trying, I require a random number between 1 and 28. That's easy with other BASIC-type languages, but what is the best way with bB?

Thanks.

936890[/snapback]

There are definitely other ways, but what comes to my mind is this.

```10 a=rand:if a>215 then 10
a=a/8:a=a+1
```

Edited by batari
##### Share on other sites

Oops, I made a mistake - the above will give a number from 1 to 27.

This should give you 1 to 28:

```10 a=rand:if a>223 then 10
a=a/8:a=a+1
```

##### Share on other sites

Oops, I made a mistake - the above will give a number from 1 to 27.

This should give you 1 to 28:

```10 a=rand:if a>223 then 10
a=a/8:a=a+1
```

936947[/snapback]

Thanks. Would that give me a problem if a was 0 because of the division? Whether or not it's a problem with bB, would this work OK?

``` a=rand : if a<9 || a>223 then a=223
a=a/8 : a=a+1
```

Edited by Random Terrain
##### Share on other sites

Oops, I made a mistake - the above will give a number from 1 to 27.

This should give you 1 to 28:

```10 a=rand:if a>223 then 10
a=a/8:a=a+1
```

936947[/snapback]

Thanks. Would that give me a problem if a was 0 because of the division? Whether or not it's a problem with bB, would this work OK?

Doing 0/8 is not a problem. Doing something like 8/0 will generate an error in most languages, but not bB. It traps division by zero and just returns zero.

``` a=rand : if a<9 || a>223 then a=223
a=a/8 : a=a+1
```

936970[/snapback]

The above would produce a number from 2 to 28, and would strongly favor 28 (which would occur roughly 20% of the time, as opposed to around 3% for the other digits.)

##### Share on other sites

Doing 0/8 is not a problem. Doing something like 8/0 will generate an error in most languages, but not bB.  It traps division by zero and just returns zero.

That's great news. That sounds like something to add to the help file.

##### Share on other sites

I need to change my web site over to the new look and work on my version of the bB help page some more, but I need to try something with bB before I forget about it. For this thing I'm trying, I require a random number between 1 and 28. That's easy with other BASIC-type languages, but what is the best way with bB?

Thanks.

936890[/snapback]

Fred already gave you a way to do it, but here's another idea:

``` a=rand
reduce_a
if a>28 then a=a-28:goto reduce_a
```

Michael Rideout

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.