# random • ### Question

• is it possible to generate random number in SB?

or could you please explain to me how to get time from pc clock?

thank you

Sunday, January 27, 2013 9:03 PM

• There is the method GetRandomNumber which returns a random integer value between 1 and the input argument.  Note that if for example you want a random number between -10 and 10 with possible values in steps of 0.1 you just do some arithmetic on the random number generated.

```'randon numbers  bewteen -10 and 10 with steps size of 0.1
For i = 1 To 20
rand = (Math.GetRandomNumber(201)-101)/10
TextWindow.WriteLine(rand)
EndFor```

Also see the Clock methods for current time, including Clock.EllapsedMilliseconds for the current pc clock time.

Sunday, January 27, 2013 9:12 PM
• Yes, that is a way to do it, but the "For i = 1 To 1" and the "EndFor" are not necessary for it to work. Another thing to note is that if you want a random number from 1 to 100 you wouldn't need the -1 part. So in your case, a better way to do it would be rand = Math.GetRandomNumber(100). And yes, it is possible to generate a psuedo-random number from the clock. You would use the class "Clock." For example, if you wanted to use the milliseconds of the clock, you would use Clock.Milliseconds.

I am an 11 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'

Sunday, January 27, 2013 9:44 PM
• Your code generates 1 random number between 0 and 99.

For i = 1 To 1 ' This just creates one number so probably unnecessary

Math.GetRandomNumber(100) ' This gets a random number between 1 and 100 (inclusive)

(Math.GetRandomNumber(100) - 1) ' This is just then a random number between 0 and 99 (inclusive)

It is possible to create a random number from from the PC clock, but you have to wait an indeterminate time between calls to the clock to be sure you get a new random number and then only consider the small (ms) values.  It may not also be truly random depending on how you create a delay and maybe other factors on how the clock is updated and used.

Also see last month's challenges where some of this was explored, here.

Sunday, January 27, 2013 9:47 PM
• This right, except the last line that should be (note the 2nd argument is a 2:

`File.WriteLine("C:\SB\variables.txt", 2, var2)The file is created if it doesn't exist.`

Sunday, January 27, 2013 10:15 PM
• Use an if statelemet

```If (var >= 1 And var <=50) Then
'Do somethign if var is between 1 and 50 inclusive
EndIf```

Monday, January 28, 2013 1:45 PM

### All replies

• There is the method GetRandomNumber which returns a random integer value between 1 and the input argument.  Note that if for example you want a random number between -10 and 10 with possible values in steps of 0.1 you just do some arithmetic on the random number generated.

```'randon numbers  bewteen -10 and 10 with steps size of 0.1
For i = 1 To 20
rand = (Math.GetRandomNumber(201)-101)/10
TextWindow.WriteLine(rand)
EndFor```

Also see the Clock methods for current time, including Clock.EllapsedMilliseconds for the current pc clock time.

Sunday, January 27, 2013 9:12 PM
• 1. then if i want to get random number from 1 to 100 i type

For i = 1 To 1
rand = (Math.GetRandomNumber(100) - 1)
EndFor

2. so it is possible to make ''random'' number generator based on pc clock, isnt it?

Sunday, January 27, 2013 9:25 PM
• Yes, that is a way to do it, but the "For i = 1 To 1" and the "EndFor" are not necessary for it to work. Another thing to note is that if you want a random number from 1 to 100 you wouldn't need the -1 part. So in your case, a better way to do it would be rand = Math.GetRandomNumber(100). And yes, it is possible to generate a psuedo-random number from the clock. You would use the class "Clock." For example, if you wanted to use the milliseconds of the clock, you would use Clock.Milliseconds.

I am an 11 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'

Sunday, January 27, 2013 9:44 PM
• Your code generates 1 random number between 0 and 99.

For i = 1 To 1 ' This just creates one number so probably unnecessary

Math.GetRandomNumber(100) ' This gets a random number between 1 and 100 (inclusive)

(Math.GetRandomNumber(100) - 1) ' This is just then a random number between 0 and 99 (inclusive)

It is possible to create a random number from from the PC clock, but you have to wait an indeterminate time between calls to the clock to be sure you get a new random number and then only consider the small (ms) values.  It may not also be truly random depending on how you create a delay and maybe other factors on how the clock is updated and used.

Also see last month's challenges where some of this was explored, here.

Sunday, January 27, 2013 9:47 PM
• and last question, not related to the random

if i want to keep some of variables in file :

//in the beginning of a program

`var2 = File.ReadLine("C:\SB\variables.txt", 2).....//doing some computations.....File.WriteLine("C:\SB\variables.txt", 1, var1)File.WriteLine("C:\SB\variables.txt", 1, var2)//quitis it correct? and do i have to create the variables.txt by myself?if i do, is it necessary to type 1, 2, etc. in the beginnig of the lines in that file?`

Sunday, January 27, 2013 10:06 PM
• This right, except the last line that should be (note the 2nd argument is a 2:

`File.WriteLine("C:\SB\variables.txt", 2, var2)The file is created if it doesn't exist.`

Sunday, January 27, 2013 10:15 PM
• sorry for lame question, i looked through the forum but didnt find something close enough

how to check if variable belong to interval?

Monday, January 28, 2013 1:27 PM
• Use an if statelemet

```If (var >= 1 And var <=50) Then
'Do somethign if var is between 1 and 50 inclusive
EndIf```

Monday, January 28, 2013 1:45 PM
• Hi ming7,

All variables in Small Basic are global variable, you an use them anywhere withing your program.

Merry Xmas!

Monday, January 28, 2013 1:51 PM
• oh, thanks! i didnt know about 'and' in if-then

thats the soilution

can i also use 'or' the same way?
• Edited by Monday, January 28, 2013 2:51 PM
Monday, January 28, 2013 2:04 PM
• oh, thanks! i didnt know about 'and' in if-then

thats the soilution

can i also use 'or' the same way?

Yes we can use "Or" in If....Else

e.g

```If Var1 = 1 Or Var2 = 5 Then 'Do something if one of two condition is met.   'Your code goes here
Else
'Another code
EndIf```

You can use a mix of "Or" and "And"

e.g

```If Var1 > 1 And Var1 < 9 Or Var2 is 0 Then 'Do something if Var1 is > 1 and < 9 or Var2 is equal to 0
Else
'Another code
EndIf```

Edit:

When we put an "And" in If...Else, it means that both the condition must  be met to execute the codes in If...Else construct.

When we use "Or", it means that if one of the two or more* condition met the code in If...Else will execute.

*you can use as many "Or" and "And" in a single condition as you want)

Merry Xmas!

Monday, January 28, 2013 5:49 PM