How can I get a random multiple of 50 using Microsoft Small Basic?

171 views Asked by At

How can I get a random multiple of 50 between 0 and 800?

So I would need numbers:
0,50,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800.

I've tried using math.getrandomnumber(800) but that gives me any number.

3

There are 3 answers

0
Emil Vikström On

Get a random number between 0 and 16, then multiply it with 50.

1
PIERRE NAJEM On

Firstly You Should get a random number under 16. Then multiply the random number under 16 x 50. Like that you will get always a random multiple of 50 under 800. Because 50 x 16 = 800. And 16 is the maximal number that you can multiply with 50.

RandomNumber_under16 = Math.GetRandomNumber(16)

random_multiple_of_50_under_800 = RandomNumber_under16*50

TextWindow.WriteLine(random_multiple_of_50_under_800)
0
Sep Roland On

So I would need numbers, 0,50,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800

You can build these numbers by scaling the numbers in the range [0,16] by a factor of 50.

Given the definition of the Math.GetRandomNumber function

Math.GetRandomNumber(maxNumber)
Gets a random number between 1 and the specified maxNumber (inclusive).
Parameters
maxNumber: The maximum number for the requested random value.
Returns
A Random number that is less than or equal to the specified max.

your solution will have to account for the fact that the returned random integer starts at 1 whereas you need to include 0 in your list.
Next code snippet produces the desired result:

For i = 1 To 20
  TextWindow.WriteLine((Math.GetRandomNumber(17) - 1) * 50)
EndFor
' Math.GetRandomNumber(17)              -> [1,17]
' Math.GetRandomNumber(17) - 1          -> [0,16]
' (Math.GetRandomNumber(17) - 1) * 50   -> {0,50,100,150, ... ,800}