# Random number

• ### Question

• hi! is tehre a way to get a random number from example 2 to 5? instead of 1 to X?
and is it possible to get a random number from 5 to -5? cuz i really need this ...
Live for nothing, or game for something!
Friday, August 14, 2009 9:03 PM

• get a random number from example 2 to 5
randNum = Math.GetRandomNumber(4) + 1

get a random number from 5 to -5
randNum = Math.GetRandomNumber(10) - 5

• Proposed as answer by Friday, August 14, 2009 11:57 PM
• Marked as answer by Saturday, August 15, 2009 12:00 AM
Friday, August 14, 2009 9:22 PM

### All replies

• get a random number from example 2 to 5
randNum = Math.GetRandomNumber(4) + 1

get a random number from 5 to -5
randNum = Math.GetRandomNumber(10) - 5

• Proposed as answer by Friday, August 14, 2009 11:57 PM
• Marked as answer by Saturday, August 15, 2009 12:00 AM
Friday, August 14, 2009 9:22 PM

• randNum = Math.GetRandomNumber(10) - 5

and this does what?

Live for nothing, or game for something!
Friday, August 14, 2009 9:23 PM
• the random number generator will give you numbers from 1 to 10, then when you subtract it by 5, it will give you random number from -5 to 5

Friday, August 14, 2009 9:24 PM
• the random number generator will give you numbers from 1 to 10, then when you subtract it by 5, it will give you random number from -5 to 5

uhm thx but, Math.GetRandomNumber(10) - 5 gave me just -5 all the time and Math.GetRandomNumber(4) + 1
gave me 11,21,31 and 41 ....
Live for nothing, or game for something!
Friday, August 14, 2009 9:28 PM
• Try to run this first to get random number from -5 to 5

For i = 1 To 20
randNum = Math.GetRandomNumber(10) - 5
TextWindow.WriteLine(randNum)
EndFor

and for 2 t0 5

For i = 1 To 20
randNum = Math.GetRandomNumber(4) + 1
TextWindow.WriteLine(randNum)
EndFor

Maybe there's a logic error on your code.

Can you post your code?

Friday, August 14, 2009 9:33 PM
• A minor point:

Math.GetRandomNumber(10) - 5 is random number between -4 and 5

This should be:

randNum = Math.GetRandomNumber(11) - 6
Friday, August 14, 2009 9:38 PM
• Try to run this first to get random number from -5 to 5

For i = 1 To 20
randNum = Math.GetRandomNumber(10) - 5
TextWindow.WriteLine(randNum)
EndFor

and for 2 t0 5

For i = 1 To 20
randNum = Math.GetRandomNumber(4) + 1
TextWindow.WriteLine(randNum)
EndFor

Maybe there's a logic error on your code.

Can you post your code?

wtf? i tried following in my code:

Textwindow.writeline ("Random: " + Math.GetRandomNumber(4)+1)

in a loop... but yours worked when i tried it in a new project....

i'll upload the project if you want

Live for nothing, or game for something!
Friday, August 14, 2009 9:38 PM
• here you go:
FMF634

it still got many bugs..^^

controls are: arrow keys for movement, down arrow for Throwing the "ball" and space for shooting...
Live for nothing, or game for something!
Friday, August 14, 2009 9:40 PM
• This is because it is treating Math.GetRandomNumber(4) and 1 as strings and concatenating them, probably because of the "Random" string at the start of the output telling the compiler that it is expecting strings.

Try: Note the brackets

For i = 1 To 50
Textwindow.writeline ("Random: " + ( Math.GetRandomNumber(4)+1) )
EndFor
Friday, August 14, 2009 9:42 PM
• This is because it is treating Math.GetRandomNumber(4) and 1 as strings and concatenating them, probably because of the "Random" string at the start of the output telling the compiler that it is expecting strings.

Try: Note the brackets

For i = 1 To 50
Textwindow.writeline ("Random: " + ( Math.GetRandomNumber(4)+1) )
EndFor
oh dam! youre right! XD thx so much for the help!! XD
btw, if you tried my code... do you know how to remove that "lag" when i shoot and move or jump around?

Live for nothing, or game for something!
Friday, August 14, 2009 9:44 PM
• As a better explanation when the compiler sees it has to print:

"Random: " + Math.GetRandomNumber(4)+1

"Random: " + Math.GetRandomNumber(4)

This is then a string, say "Ramdom: 3", it then adds '1'.  The can't add a number to a string so it treats them both a stings and you get "Ramdom: 31".  The brackets does the arithmetic first, then adds it to the string (concatenates it) and gives "Ramdom: 4".

Looking at you program, suggestions:

Don't use variable names to be the same a Subroutine names (e.g. PlayerTouchingGround).
Don't use keywords as Subroutine names (e.g. TextWindow())

Also, the keyboard jerkiness can arise as a result of the keyboard repeat delay in the OS.  To get round this games writers have done the following.

1] Use Keydown and Keyup envents to flag the status of the keys.
2] Just set key status flags within the event subroutines and do the actions on these status in the main loop.

I suggest looking carefully at some of the sample games (Asteroids and Startgates and Game Graphics - these examples deal with just these issues), and starting with a very simple game to get the movement smooth, then extend to the game you want.

GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp

x = 100
y = 100
left = 0
right = 0
up = 0
down = 0

While ("True")
If (left = 1) Then
x = x-1
EndIf
If (right = 1) Then
x = x+1
EndIf
If (up = 1) Then
y = y-1
EndIf
If (down = 1) Then
y = y+1
EndIf
Shapes.Move(ball,x-25,y-25)
Program.Delay(5)
EndWhile

Sub OnKeyDown
Key = GraphicsWindow.LastKey
If (Key = "Left") Then
left = 1
ElseIf (Key = "Right") Then
right = 1
ElseIf (Key = "Up") Then
up = 1
ElseIf (Key = "Down") Then
down = 1
EndIf
EndSub

Sub OnKeyUp
Key = GraphicsWindow.LastKey
If (Key = "Left") Then
left = 0
ElseIf (Key = "Right") Then
right = 0
ElseIf (Key = "Up") Then
up = 0
ElseIf (Key = "Down") Then
down = 0
EndIf
EndSub

Friday, August 14, 2009 10:17 PM
• As a better explanation when the compiler sees it has to print:

"Random: " + Math.GetRandomNumber(4)+1

"Random: " + Math.GetRandomNumber(4)

This is then a string, say "Ramdom: 3", it then adds '1'.  The can't add a number to a string so it treats them both a stings and you get "Ramdom: 31".  The brackets does the arithmetic first, then adds it to the string (concatenates it) and gives "Ramdom: 4".

Looking at you program, suggestions:

Don't use variable names to be the same a Subroutine names (e.g. PlayerTouchingGround).
Don't use keywords as Subroutine names (e.g. TextWindow())

Also, the keyboard jerkiness can arise as a result of the keyboard repeat delay in the OS.  To get round this games writers have done the following.

1] Use Keydown and Keyup envents to flag the status of the keys.
2] Just set key status flags within the event subroutines and do the actions on these status in the main loop.

I suggest looking carefully at some of the sample games (Asteroids and Startgates and Game Graphics - these examples deal with just these issues), and starting with a very simple game to get the movement smooth, then extend to the game you want.

GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp

x = 100
y = 100
left = 0
right = 0
up = 0
down = 0

While ("True")
If (left = 1) Then
x = x-1
EndIf
If (right = 1) Then
x = x+1
EndIf
If (up = 1) Then
y = y-1
EndIf
If (down = 1) Then
y = y+1
EndIf
Shapes.Move(ball,x-25,y-25)
Program.Delay(5)
EndWhile

Sub OnKeyDown
Key = GraphicsWindow.LastKey
If (Key = "Left") Then
left = 1
ElseIf (Key = "Right") Then
right = 1
ElseIf (Key = "Up") Then
up = 1
ElseIf (Key = "Down") Then
down = 1
EndIf
EndSub

Sub OnKeyUp
Key = GraphicsWindow.LastKey
If (Key = "Left") Then
left = 0
ElseIf (Key = "Right") Then
right = 0
ElseIf (Key = "Up") Then
up = 0
ElseIf (Key = "Down") Then
down = 0
EndIf
EndSub

oh man! thx so much! youre such a great dude! your code is awesome! i'll look trough it good soon!!

Live for nothing, or game for something!
Friday, August 14, 2009 10:23 PM
• While ("True")
If (left = 1) Then
x = x-1
EndIf
If (right = 1) Then
x = x+1
EndIf
If (up = 1) Then
y = y-1
EndIf
If (down = 1) Then
y = y+1
EndIf
Shapes.Move(ball,x-25,y-25)
Program.Delay(5)

sorry, but you used "While ("True")" and i saw that before somewhere, whats that for? i mean, While what is "true"?

Live for nothing, or game for something!
Friday, August 14, 2009 10:31 PM
• While (something)
.
.
.
EndWhile

This will do whatever is inside the While .. EndWhile as log as the something is true.

The something could be a statement such as 'i < 100 ' or 'time < 100 ' etc.  The "True " is a special phrase that is equivalent to true.  (There is also "False" that is always false).

Hence this loop runs forever, we could have used:

While (1 = 1)
or anything else that always evaluates to true.

"True " evalues to true, just as 1 = 1 evaluates to true , or even 1 <> 2 is true.
Friday, August 14, 2009 10:47 PM
• While (something)
.
.
.
EndWhile

This will do whatever is inside the While .. EndWhile as log as the something is true.

The something could be a statement such as 'i < 100 ' or 'time < 100 ' etc.  The "True " is a special phrase that is equivalent to true.  (There is also "False" that is always false).

Hence this loop runs forever, we could have used:

While (1 = 1)
or anything else that always evaluates to true.

"True " evalues to true, just as 1 = 1 evaluates to true , or even 1 <> 2 is true.
couldnt we just use

loop:
goto loop

then?

Live for nothing, or game for something!
Friday, August 14, 2009 10:56 PM
• Yes we could - there is however a general dislike for using GOTO among programmers.  It can jump from place to place and consequently make code extremely hard to follow -  'spaghetti' code.  It can be useful, especially in Small Basic, where there are limited other flow control constructs, but as a rule I would suggest not using GOTO if there is another reasonable alternative.

Note that if you use GOTO, you cannot use itto jump into or out of a Subroutine.

For example - debug this mess:

x = 0
y = 0
For i = 1 To 100
a:
x = x+2
c:
y = y+1
If (i < 10) Then
Goto a
else
y = y+1
Goto b
EndIf
If (y < 3) Then
Goto c
EndIf
b:
x = x-1
EndFor

TextWindow.WriteLine(x+" "+y)

Friday, August 14, 2009 11:11 PM
• Yes we could - there is however a general dislike for using GOTO among programmers.  It can jump from place to place and consequently make code extremely hard to follow -  'spaghetti' code.  It can be useful, especially in Small Basic, where there are limited other flow control constructs, but as a rule I would suggest not using GOTO if there is another reasonable alternative.

Note that if you use GOTO, you cannot use itto jump into or out of a Subroutine.

For example - debug this mess:

x = 0
y = 0
For i = 1 To 100
a:
x = x+2
c:
y = y+1
If (i < 10) Then
Goto a
else
y = y+1
Goto b
EndIf
If (y < 3) Then
Goto c
EndIf
b:
x = x-1
EndFor

TextWindow.WriteLine(x+" "+y)

lol! that doesnt even run! XD but i didnt mean goto for use instead of sub routines, i meant it as use instead of while..

Live for nothing, or game for something!
Friday, August 14, 2009 11:13 PM
• The code above is intentionally rubbish - just an example of the mess that can be caused by using GOTO indiscriminately ('debug this mess' was not meant literally, it was meant to show that indiscriminate use of GOTO's can be very hard to follow).
The comments on jumping in or out of subroutines is just a passing remark, because this has caused problems before.

You can use GOTO instead of the while and you should if you are happier with it - the only point is that if there is a reasonable alternative (as the while is) it is generally considered good practice.
• Edited by Friday, August 14, 2009 11:25 PM
Friday, August 14, 2009 11:22 PM
• The code above is intentionally rubbish - just an example of the mess that can be caused by using GOTO indiscriminately.
The comments on jumping in or out of subroutines is just a passing remark, because this has caused problems before.

You can use GOTO instead of the while and you should if you are happier with it - the only point is that if there is a reasonable alternative (as the while is) it is generally considered good practice.
ok, so it actually doesnt make a difference right? but while is smarter to use as practice?

Live for nothing, or game for something!
Friday, August 14, 2009 11:24 PM
• Yes
Friday, August 14, 2009 11:26 PM
• Yes
ok! thx alot!

Live for nothing, or game for something!
Friday, August 14, 2009 11:27 PM
• thx again for your awesome keyboard key code! its so simple but effective! my game works so great now! THX MAN!!!
Live for nothing, or game for something!
Saturday, August 15, 2009 12:35 AM