hexagon logo

How do I use a random number generator?

I would like to create a random number. I have tried using the RND command but it returns a value of zero.

Has anyone tried to create a random number and assign it to a variable in PCDMIS?

Thanks
  • Put the following in a text file, call it Random.BAS. Each time you call the function NextRandom the PC-DMIS variable RND will have a new value (between 0 and 1).

    Sub NextRandom
    
     Dim App As Object
      Set App = CreateObject ("PCDLRN.Application")
      Dim Part As Object
      Set Part = App.ActivePartProgram
      Dim Var As Object
      Set Var = Part.GetVariableValue ("RND")
      Dim I As Object
      If Not Var Is Nothing Then
        Var.DoubleValue = RND(1)
        Part.SetVariableValue "RND", Var
        MsgBox "RND is now: " & Var.DoubleValue
      Else
        MsgBox "Could Not find variable"
      End If
    
    End Sub
    
    Sub InitRandom
     RANDOMIZE
     Call NextRandom
    End Sub
    
    Sub Main
      Call NextRandom	
    End Sub
    
  • Can you explain how the If-Then block beginning with "If Not Var is Nothing..." Works?

    What is the variable "I" being declared for?
  • Can you explain how the If-Then block beginning with "If Not Var is Nothing..." Works?


    Var is assigned as a copy of the PC-DMIS variable "RND" - if that variable name doesn't exist in your program, this code will not create it, but give the error message that the variable can not be found.

    If the name is found, Var gets a new random value between 0.000 and 0.999... from RND(1), and then this new value is sent back to the PC-DMIS variable.

    What is the variable "I" being declared for?


    Just to check that you are actually reading the code Slight smile In other words - I haven't the faintest idea, must be a reminiscence of something else I tried.
  • Ok, I understand now. There is a PCDMIS variable named RND and there is a cypress enable function that is also named RND.
  • Hi,Anders,Can I enter the command 'NextRandom' in PC-DMIS command modle?And how to use this script.

    Put the following in a text file, call it Random.BAS. Each time you call the function NextRandom the PC-DMIS variable RND will have a new value (between 0 and 1).

    Sub NextRandom
    
     Dim App As Object
      Set App = CreateObject ("PCDLRN.Application")
      Dim Part As Object
      Set Part = App.ActivePartProgram
      Dim Var As Object
      Set Var = Part.GetVariableValue ("RND")
      Dim I As Object
      If Not Var Is Nothing Then
        Var.DoubleValue = RND(1)
        Part.SetVariableValue "RND", Var
        MsgBox "RND is now: " & Var.DoubleValue
      Else
        MsgBox "Could Not find variable"
      End If
    
    End Sub
    
    Sub InitRandom
     RANDOMIZE
     Call NextRandom
    End Sub
    
    Sub Main
      Call NextRandom	
    End Sub
    
  •             ASSIGN/RND=0
    CS1        =SCRIPT/FILENAME= C:\USERS\PUBLIC\DOCUMENTS\WAI\PC-DMIS\2014\REPORTING\RANDOM.BAS
                FUNCTION/InitRandom,SHOW=YES,,
                STARTSCRIPT/
                ENDSCRIPT/
                COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,
                First random value
                RND
    CS2        =SCRIPT/FILENAME= C:\USERS\PUBLIC\DOCUMENTS\WAI\PC-DMIS\2014\REPORTING\RANDOM.BAS
                FUNCTION/NextRandom,SHOW=YES,,
                STARTSCRIPT/
                ENDSCRIPT/
                COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,
                Second random value
                RND
    CS2        =SCRIPT/FILENAME= C:\USERS\PUBLIC\DOCUMENTS\WAI\PC-DMIS\2014\REPORTING\RANDOM.BAS
                FUNCTION/NextRandom,SHOW=YES,,
                STARTSCRIPT/
                ENDSCRIPT/
                COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,
                Third random value
                RND