locked
Random number issue RRS feed

  • Question

  • I tried to see what I was doing wrong but for the sake of it I can not understand the error message TYPE EXPECTED for random function. I tried it with and without the parens but no good.

    Its probably something simple as always but I am puzzled.

    Les

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim dum As Integer
            Dim rn As New Random()
            dum = rn.Next(0, 20)
        End Sub
    End Class

    Thanks in advance

    Les


    • Edited by Les2011 Tuesday, June 23, 2020 5:49 PM
    Tuesday, June 23, 2020 5:48 PM

Answers

  • Remove the parenthetical s after Random.

    If you are going to use Random in an app declare it once and use it.  Like this

        Private Shared PRNG As New Random
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim i As Integer = PRNG.Next(0, 20)
        End Sub
    


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    • Marked as answer by Les2011 Tuesday, June 23, 2020 8:24 PM
    Tuesday, June 23, 2020 6:13 PM
  • Hi

    Try changing the line to this and see if any improvement.

      Private Shared PRNG As New System.Random
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Les2011 Tuesday, June 23, 2020 8:21 PM
    Tuesday, June 23, 2020 7:57 PM

All replies

  • Remove the parenthetical s after Random.

    If you are going to use Random in an app declare it once and use it.  Like this

        Private Shared PRNG As New Random
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim i As Integer = PRNG.Next(0, 20)
        End Sub
    


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    • Marked as answer by Les2011 Tuesday, June 23, 2020 8:24 PM
    Tuesday, June 23, 2020 6:13 PM
  • Hi John,

    Well I tried your code snippet and this is a pic as to what I am getting.

    Still getting this error??

    Tuesday, June 23, 2020 6:27 PM
  • Hi John,

    Well I tried your code snippet and this is a pic as to what I am getting.

    Still getting this error??

    Hi

    Tried that exact code and everything works as expected here with no exceptions

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Shared PRNG As New Random
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i As Integer = PRNG.Next(0, 20)
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, June 23, 2020 7:00 PM
    Tuesday, June 23, 2020 6:59 PM
  • Hi Les,

    Yeah this is strange because I have been using similar code in the past???  I am going to bail out of vb and restart my computer and will report back as to what happens??

    Hope your doing well Les and you and your family are safe.  I will be back in a few

    thx,

    Les

    Tuesday, June 23, 2020 7:04 PM
  • Hi Les,

    Well I rebooted and launched vb and same problem??? Here is the snap shot of what I tried to run with the error showing

    I have no idea what is going on.  Do I need some kind of header etc???

    THx

    Les

    Tuesday, June 23, 2020 7:48 PM
  • Hi

    Try changing the line to this and see if any improvement.

      Private Shared PRNG As New System.Random
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Les2011 Tuesday, June 23, 2020 8:21 PM
    Tuesday, June 23, 2020 7:57 PM
  • Hi Leshay,

    Ok I think I figured it out and I really don't know why if this is in fact the issue.  When I created the test app I created it with the name RANDOM.  If I copy my original code I sent you and created the file with the name RANDOMGEN the issue disappears??  What does the file name have to do with the error that vb is reporting?  Try my code by creating a project with the name random and then create a project with the name randongen and see if you see what I see, just curious.

    Thx so much for your help

    Les

    Tuesday, June 23, 2020 8:03 PM
  • Hi Leshay,

    Good thought that did the trick adding the system.random 

    Thanks Leshay

    Les


    • Edited by Les2011 Tuesday, June 23, 2020 8:21 PM
    Tuesday, June 23, 2020 8:19 PM
  • Hi dba,

    Your code snippet works but what I was missing was the identifier system.random

    Thanks

    Les

    Tuesday, June 23, 2020 8:23 PM