# Step statement not working correctly

• ### Question

• When I try to generate random even numbers between 2 to 28 with the 'Step' statement; it doesn't work/why?

This is how I am doing it.

For a=2 to 28 step 2

rndnum=Math.GetRandomnumber(a)

EndFor

I'm obtaining odd numbers as well as even numbers, what am I doing wrong?

Thanks

Tuesday, March 18, 2014 10:33 PM

• Math.GetRandomNumber() picks a random whole number from the range from 1 to the argument passed.

Of course, that range includes both even & odd numbers! Passing an even argument won't prevent it from returning odd values too! It's actually 50-50, and includes at least the lowest odd 1!

If you really want even numbers only, you'll have to check whether picked value is odd. For that, use Math.Remainder() w/ 2 as the divisor argument and see whether its remaining is 1.

Then increase confirmed odd value by 1 in order to force it being even! Take a look at the example below:

```'http://social.msdn.microsoft.com/Forums/en-US
'/7fe64db4-cd4c-4833-9a61-5bc687f50d3a
'/step-statement-not-working-correctly

TB = Text.GetCharacter(9)

For i = 2 To 28 Step 2
rnd = Math.GetRandomNumber(i)

If Math.Remainder(rnd, 2) = 1 Then
rnd = rnd + 1
EndIf

TextWindow.WriteLine("Range: " + i + TB + "Picked: " + rnd)
EndFor

TextWindow.WriteLine("")```

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

• Edited by Tuesday, March 18, 2014 11:44 PM
• Proposed as answer by Wednesday, March 19, 2014 9:40 AM
• Marked as answer by Wednesday, March 19, 2014 3:40 PM
Tuesday, March 18, 2014 11:28 PM

### All replies

• Math.GetRandomNumber() picks a random whole number from the range from 1 to the argument passed.

Of course, that range includes both even & odd numbers! Passing an even argument won't prevent it from returning odd values too! It's actually 50-50, and includes at least the lowest odd 1!

If you really want even numbers only, you'll have to check whether picked value is odd. For that, use Math.Remainder() w/ 2 as the divisor argument and see whether its remaining is 1.

Then increase confirmed odd value by 1 in order to force it being even! Take a look at the example below:

```'http://social.msdn.microsoft.com/Forums/en-US
'/7fe64db4-cd4c-4833-9a61-5bc687f50d3a
'/step-statement-not-working-correctly

TB = Text.GetCharacter(9)

For i = 2 To 28 Step 2
rnd = Math.GetRandomNumber(i)

If Math.Remainder(rnd, 2) = 1 Then
rnd = rnd + 1
EndIf

TextWindow.WriteLine("Range: " + i + TB + "Picked: " + rnd)
EndFor

TextWindow.WriteLine("")```

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

• Edited by Tuesday, March 18, 2014 11:44 PM
• Proposed as answer by Wednesday, March 19, 2014 9:40 AM
• Marked as answer by Wednesday, March 19, 2014 3:40 PM
Tuesday, March 18, 2014 11:28 PM
• Or

`mdnum = Math.GetRandomNumber(14) * 2`

Nonki Takahashi

Wednesday, March 19, 2014 9:39 AM
• Hi greenboy

Your sample is using "a" to set the argument for the Math.GetRandomNumber function.

Your For loop will set "a" as 2,4,6,8.....28 as it loops.

If you use intellisense to see how the GetRandomNumber function works it shows:

Gets a random number between 1 and the specified maxNumber (inclusive). Math.GetRandomNumber(maxNumber)

So if "a" is = 28 then you're setting the maximum number as 28 and Math.RandomNumber(a) will return a random number anywhere between 1 an 28 inclusive.

There's slightly different ways to do this by using the For loop. Here's a sample that will return 10 even random numbers between 2 and 28 (not including 2 and 28 themselves).

```minNumber = 3
maxNumber = 27

For i = 1 To 10  'loops 10 times with default Step of 1
randomNumber = minNumber + Math.GetRandomNumber(maxNumber - minNumber)
If Math.Remainder(randomNumber, 2) = 0 Then
TextWindow.WriteLine(randomNumber)  'write the even number to TextWindow as we go
Else
i = i - 1  'do For loop again if Not an even number by reducing i
EndIf
EndFor```

And you could also store the 10 randomNumbers in a container/Array and write them out later or manipulate them.

```minNumber = 3
maxNumber = 27

For i = 1 To 10  'loops 10 times with default Step of 1
randomNumber = minNumber + Math.GetRandomNumber(maxNumber - minNumber)
If Math.Remainder(randomNumber, 2) = 0 Then
evenRandomNumber[i] = randomNumber  'stores the evenNumber in an Array
Else
i = i - 1  'do For loop again if Not an even number
EndIf
EndFor

TextWindow.Pause()
TextWindow.WriteLine("Write contents of what's stored in the Array")

For i = 1 To Array.GetItemCount(evenRandomNumber)
TextWindow.WriteLine(evenRandomNumber[i])
EndFor```

• Edited by Wednesday, March 19, 2014 10:27 AM
Wednesday, March 19, 2014 10:19 AM
• Thanks everyone for your help, I tried the Math.GetRemainder statement but, for some reason It kept returning ( 0 ) all the time.  Obviously I didn't do it right.

Greenboy

Wednesday, March 19, 2014 3:43 PM
• `mdnum = Math.GetRandomNumber(14) * 2`
Wow! I've completely forgotten that multiplying a number by 2 always results an even number!  :D

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Wednesday, March 19, 2014 4:47 PM
• Math.GetRemainder() is the remainder or modulo of a division operation.

When the 2nd argument (divisor) is 2, it results either 0 or 1:

0 -> for when the 1st argument (dividend) is even.

1 -> for when it's odd.

You can learn a lot just by looking at the intellisense mini-explanation at the right side of the SB's IDE.

But the simplest solution is multiply the picked number by 2. This way, it's always even too!  :D

```' Pick a random even number between 2 & 28:

range = 28
rnd = Math.GetRandomNumber(range/2) * 2

TextWindow.WriteLine(rnd)```

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Wednesday, March 19, 2014 5:09 PM
• Math.GetRemainder() is the remainder or modulo of a division operation.

When the 2nd argument (divisor) is 2, it results either 0 or 1:

0 -> for when the 1st argument (dividend) is even.

1 -> for when it's odd.

You can learn a lot just by looking at the intellisense mini-explanation at the right side of the SB's IDE.

But the simplest solution is multiply the picked number by 2. This way, it's always even too!  :D

```' Pick a random even number between 2 & 28:

range = 28
rnd = Math.GetRandomNumber(range/2) * 2

TextWindow.WriteLine(rnd)```

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

This thread would make a great Wiki article!

http://social.technet.microsoft.com/wiki/contents/articles/23330.technet-guru-contributions-for-march.aspx

Also, GoToLoop, you're invited to join the Small Basic Community Council. Please email me (edprice at Microsoft) or add me on Skype (edwardnprice) if you're interested. Thanks!

Ed Price, Power BI & SQL Server Customer Program Manager (Blog, Small Basic, Wiki Ninjas, Wiki)