Results 1 to 11 of 11

Thread: Random Numbers The Same

  1. #1
    Hyperactive Member
    Join Date
    Dec 11
    Posts
    346

    Random Numbers The Same

    The (pseudo) random numbers created by this are always the same. Can I change that?
    Code:
        Public Function Rand(ByVal Low As Long, _
                         ByVal High As Long) As Long
            Rand = Int((High - Low + 1) * Rnd) + Low
        End Function

  2. #2
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,334

    Re: Random Numbers The Same

    Would this suffice?
    Code:
    Public Function randomNumber(ByVal low As Integer, ByVal high As Integer) As Integer
            Dim r As New Random
            randomNumber = r.Next(low, high)
    
            Return randomNumber
        End Function
    Edit - Also, try including the last low in the CInt conversion and see if that works for you:
    Code:
    Rand = CInt((High - Low + 1) * Rnd() + Low)
    I noticed it looked almost like the example given in the MSDN's library on the Rnd() function, but not quite.
    Last edited by dday9; Aug 19th, 2012 at 10:36 PM.
    Vb.Net Contributions:-Here-
    Game Contributions:-Here- New - 2d map creator
    XNA in Vb.Net Tutorials:-Here-

    Links:
    LegalShield | AUP

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,249

    Re: Random Numbers The Same

    Have you executed Randomize once and once only? What Low and High values are you using? Does it work for Integers instead of Longs? Do you definitely need to use Longs?

  4. #4
    Frenzied Member Evil_Giraffe's Avatar
    Join Date
    Aug 02
    Location
    Suffolk, UK
    Posts
    1,915

    Re: Random Numbers The Same

    Quote Originally Posted by dday9 View Post
    Would this suffice?
    Code:
    Public Function randomNumber(ByVal low As Integer, ByVal high As Integer) As Integer
            Dim r As New Random
            randomNumber = r.Next(low, high)
    
            Return randomNumber
        End Function
    Don't do this. This is wrong. Do not construct the Random instance in the function. Your application should only ever create one Random instance and all places that need to use it should be passed a reference. With the above function, if it is called twice in succession it will return the same number, as both calls will seed the Random with the same value.

  5. #5
    Hyperactive Member
    Join Date
    Dec 11
    Posts
    346

    Re: Random Numbers The Same

    I also tried this and they were always the same either. This is actualy randomizing a number for 3 diferent variables and you can keep clicking a button which would re randomize there number.
    Code:
    randomnumber = (Str(Int(Rnd() * 11 + 1)))

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,249

    Re: Random Numbers The Same

    How come you can post back but you can't answer my questions? I'd kinda like to help but can't without information.

  7. #7
    Hyperactive Member
    Join Date
    Dec 11
    Posts
    346

    Re: Random Numbers The Same

    Well I dont know if I executed it once and only once.(Im not that good with vb) My low is 1 my high is 12. I have not tried with longs. But explain why if I did why would that work better then an integer?

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,249

    Re: Random Numbers The Same

    Quote Originally Posted by sherlockturtle View Post
    Well I dont know if I executed it once and only once.
    Surely you know whether you executed it at all. If you haven't then obviously you haven't executed it once. If you have executed it then could you not simply put a MessageBox.Show right after it and if you only see that once then you know that you've only executed it once? Failing that could you not show us where it is and let us try to work it out? Don't make us drag every little bit of information out of you.
    Quote Originally Posted by sherlockturtle View Post
    My low is 1 my high is 12. I have not tried with longs. But explain why if I did why would that work better then an integer?
    Um, you HAVE tried it with Longs. Here's your own code:
    Code:
        Public Function Rand(ByVal Low As Long, _
                         ByVal High As Long) As Long
            Rand = Int((High - Low + 1) * Rnd) + Low
        End Function
    You shouldn't be using Longs at all if all you need is Integers, which is exactly why I asked those questions. Now that we actually know that you just need Integers then you can basically throw that VB6-esque code away and do this thr proper way. Presumably this code is in a form. At the top of the form, declare a variable of type Random, create an instance of the Random class and assign that instance to the variable, e.g.
    Code:
    'Create a random number generator.
    Private rng As New Random
    Now, every time you need a random number, you simply call the Next method of that object, e.g.
    Code:
    Dim number As Integer = rng.Next(1, 13)
    After that code the 'number' variable will contain in the range of 1 to 12 inclusive. Note that the minimum value passed to Next is inclusive while the maximum is exclusive, which is why you must pass 13 as the maximum to the method to be returned a maximum value of 12.
    Last edited by jmcilhinney; Aug 20th, 2012 at 09:08 AM.

  9. #9
    Hyperactive Member
    Join Date
    Dec 11
    Posts
    346

    Re: Random Numbers The Same

    Thanks, worked. Sorry im just not that good with vb.

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,249

    Re: Random Numbers The Same

    Quote Originally Posted by sherlockturtle View Post
    Thanks, worked. Sorry im just not that good with vb.
    That's fine but that makes it even more important to give us as much relevant information as you can, which includes answering questions when they're asked. If there's some legitimate reason that you can't provide the answer then that's fine too but if you post and don't even acknowledge the question then that comes across as your ignoring it.

  11. #11
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,334

    Re: Random Numbers The Same

    Quote Originally Posted by Evil_Giraffe View Post
    Don't do this. This is wrong. Do not construct the Random instance in the function. Your application should only ever create one Random instance and all places that need to use it should be passed a reference. With the above function, if it is called twice in succession it will return the same number, as both calls will seed the Random with the same value.
    Thank you for catching that it would return the same number twice, I guess I just whipped it up in a couple of seconds with out fully thinking about it.
    Vb.Net Contributions:-Here-
    Game Contributions:-Here- New - 2d map creator
    XNA in Vb.Net Tutorials:-Here-

    Links:
    LegalShield | AUP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •