Discussion forum for the BasicX family of microcontroller chips.
|
Hi. I have a question about generating a random number. I just modified some code i found in the archives, and it works great, but i have a question about it. Here is the code: Option Explicit Private Seed As Long Sub Main() do debug.print cstr(rnd) delay(0.5) loop End Sub Public Function Rnd() As Integer If Seed Mod 2 = 0 Then Seed = Seed + 1 End If Seed = (Seed * 3125) Mod 8192 Rnd = Cint((csng(Seed))/1638.4) End Function Public Sub PutSeed(ByVal NewSeed As Single) If (NewSeed < 0.0) Then Seed = 0 Exit Sub ElseIf (NewSeed > 1.0) Then Seed = 8192 Exit Sub Else Seed = FixL(NewSeed * 8192.0) End If End Sub Okay, here are my questions. First, was does option explicit mean and do when you put it in the top of the code? Secondly, what is Mod 8192 in the rnd function part? And finally, do i even need that whole putseed sub procedure? It doesn't look like it ever calls for it in the rnd function part. Thanks. -Pat Marion _________________________________________________________________ Get your FREE download of MSN Explorer at http://explorer.msn.com |
|
|
|
option explicit it is a compiler function. the compiler will give you an error if you are tring to use a var that you have not defined. it catches typos dim myvar as new oword ..... ..... myyvar = 398 it will error on the "myyvar" because it has not been defined. this is a VB function and I think the compiler does that anyway. '---------------------------------------------------- it looks to me that the "mod" is rasing to a power of. 4^2 = 16 = 4 mod 2 But this is a guess. > Okay, here are my questions. First, was does option explicit mean > and do > when you put it in the top of the code? Secondly, what is Mod 8192 > in the > rnd function part? And finally, do i even need that whole putseed > sub > procedure? It doesn't look like it ever calls for it in the rnd > function > part. Thanks. > > -Pat Marion ===== Tony Brenke North Tacoma, WA __________________________________________________ |
|
In the computer world no number is truly random. That is why you use a seed number. Try powering up MS Excel and using the Visual Basic Macro editor, do not define a seed number and ask excel to output 10 random numbers. Save the file. Quit Excel, come back into excel open the same file and run the macro again. You will see that it spits out the exact same numbers. They will look random though.... Changing the seed number ensures that you don't obtain the same set of numbers every time you run a random command. I am not a computer scientist so I don't know the mechanics of this whole thing... But I do know that in a lot of applications you do have to change the seed number so that no two runs are the same.. One way to do this is to link the seed number to the time+date of the computer. Since time changes continously you will be assured to get a different seed number every time your run the program. Unless of course you manage to run the same program twice in less than a second.... --- In , "Pat Marion" <patmarion@h...> wrote: > Hi. I have a question about generating a random number. I just modified > some code i found in the archives, and it works great, but i have a question > about it. Here is the code: > > Option Explicit > Private Seed As Long > > Sub Main() > do > debug.print cstr(rnd) > delay(0.5) > loop > End Sub > > Public Function Rnd() As Integer > If Seed Mod 2 = 0 Then > Seed = Seed + 1 > End If > Seed = (Seed * 3125) Mod 8192 > Rnd = Cint((csng(Seed))/1638.4) > End Function > > Public Sub PutSeed(ByVal NewSeed As Single) > If (NewSeed < 0.0) Then > Seed = 0 > Exit Sub > ElseIf (NewSeed > 1.0) Then > Seed = 8192 > Exit Sub > Else > Seed = FixL(NewSeed * 8192.0) > End If > End Sub > > Okay, here are my questions. First, was does option explicit mean and do > when you put it in the top of the code? Secondly, what is Mod 8192 in the > rnd function part? And finally, do i even need that whole putseed sub > procedure? It doesn't look like it ever calls for it in the rnd function > part. Thanks. > > -Pat Marion > _________________________________________________________________ > Get your FREE download of MSN Explorer at http://explorer.msn.com |