Generating a random number

Hello.

I am a newbie to VB and have recently installed VB6. I used to do a little BASIC on my Commodore 64 but obviously that was quite some time ago! On that machine however it was easy to generate a random number, say between 0 and 10, with a simple line of code (though I can't remember what that was).

What I am looking to do is generate a genuinely random number, say between 0 and 10. How would I make 'a' for instance, have a random value between 0 and 10?

I have found the 'rnd' command but have read that the number generated would be repeated and so you use 'randomise' but to be honest I didn't really understand what it was all about. All I am looking to do is assign a genuine randomly generated value to 'a' for instance. Can you help me?

Your assistance would be appreciated.
Regards,
Chris.

Comments

  • : Hello.
    :
    : I am a newbie to VB and have recently installed VB6. I used to do a little BASIC on my Commodore 64 but obviously that was quite some time ago! On that machine however it was easy to generate a random number, say between 0 and 10, with a simple line of code (though I can't remember what that was).
    :
    : What I am looking to do is generate a genuinely random number, say between 0 and 10. How would I make 'a' for instance, have a random value between 0 and 10?
    :
    : I have found the 'rnd' command but have read that the number generated would be repeated and so you use 'randomise' but to be honest I didn't really understand what it was all about. All I am looking to do is assign a genuine randomly generated value to 'a' for instance. Can you help me?
    :
    : Your assistance would be appreciated.
    : Regards,
    : Chris.
    :
    :
    to use randomize you simply put it in the form load event of your form(you don't want it called more then once)

    then where ever you want your random number you would have something like the following:
    dim intrand as integer
    intrand = rnd * 11 ' should give you a random number between 0 and 10.


    [blue]
    C:Dos
    C:Dos Run
    Run Dos Run
    [/blue]

  • : : Hello.
    : :
    : : I am a newbie to VB and have recently installed VB6. I used to do a little BASIC on my Commodore 64 but obviously that was quite some time ago! On that machine however it was easy to generate a random number, say between 0 and 10, with a simple line of code (though I can't remember what that was).
    : :
    : : What I am looking to do is generate a genuinely random number, say between 0 and 10. How would I make 'a' for instance, have a random value between 0 and 10?
    : :
    : : I have found the 'rnd' command but have read that the number generated would be repeated and so you use 'randomise' but to be honest I didn't really understand what it was all about. All I am looking to do is assign a genuine randomly generated value to 'a' for instance. Can you help me?
    : :
    : : Your assistance would be appreciated.
    : : Regards,
    : : Chris.
    : :
    : :
    : to use randomize you simply put it in the form load event of your form(you don't want it called more then once)
    :
    : then where ever you want your random number you would have something like the following:
    : dim intrand as integer
    : intrand = rnd * 11 ' should give you a random number between 0 and 10.
    :
    :
    : [blue]
    : C:Dos
    : C:Dos Run
    : Run Dos Run
    : [/blue]
    :
    :
    Thanks very much for that - I think I'm getting the hang of random numbers now. Just a couple of points:

    Dim intrand As Integer 'know what it does but what does 'Dim' mean?

    intrand = Rnd * 11 'actually gives you a random number between 0 and 11!

    But I found if you drop the 'Dim' statement and use this instead...

    intrand = Int(Rnd * 11)

    ...it WILL give you a number between 0 and 10 as you would expect. Strange heh? Give it a go and see what I mean.

    Also 'Randomize'. I've seen it on these pages as just 'Randomize', but also as 'Randomize Timer' and 'Randomize (Timer)'. Does adding 'Timer' change 'Randomize' in any way and do the brackets make a difference?

    Cheers,
    Chris.
  • : : : Hello.
    : : :
    : : : I am a newbie to VB and have recently installed VB6. I used to do a little BASIC on my Commodore 64 but obviously that was quite some time ago! On that machine however it was easy to generate a random number, say between 0 and 10, with a simple line of code (though I can't remember what that was).
    : : :
    : : : What I am looking to do is generate a genuinely random number, say between 0 and 10. How would I make 'a' for instance, have a random value between 0 and 10?
    : : :
    : : : I have found the 'rnd' command but have read that the number generated would be repeated and so you use 'randomise' but to be honest I didn't really understand what it was all about. All I am looking to do is assign a genuine randomly generated value to 'a' for instance. Can you help me?
    : : :
    : : : Your assistance would be appreciated.
    : : : Regards,
    : : : Chris.
    : : :
    : : :
    : : to use randomize you simply put it in the form load event of your form(you don't want it called more then once)
    : :
    : : then where ever you want your random number you would have something like the following:
    : : dim intrand as integer
    : : intrand = rnd * 11 ' should give you a random number between 0 and 10.
    : :
    : :
    : : [blue]
    : : C:Dos
    : : C:Dos Run
    : : Run Dos Run
    : : [/blue]
    : :
    : :
    : Thanks very much for that - I think I'm getting the hang of random numbers now. Just a couple of points:
    :
    : Dim intrand As Integer 'know what it does but what does 'Dim' mean?
    :
    : intrand = Rnd * 11 'actually gives you a random number between 0 and 11!
    :
    : But I found if you drop the 'Dim' statement and use this instead...
    :
    : intrand = Int(Rnd * 11)
    :
    : ...it WILL give you a number between 0 and 10 as you would expect. Strange heh? Give it a go and see what I mean.
    :
    : Also 'Randomize'. I've seen it on these pages as just 'Randomize', but also as 'Randomize Timer' and 'Randomize (Timer)'. Does adding 'Timer' change 'Randomize' in any way and do the brackets make a difference?
    :
    : Cheers,
    : Chris.
    :

    No such thing as a genuinely random number (though there are a few theories, they aren't available to us) so you need to fake it. This is done by using a formula that generates what appears to be a random number. This formula is called Rnd() in VB. Randomize allows you to change the formula's starting point so that a different sequence is used. If you don't use Randomize, it starts with 0. I forget what the default value is if you use Randomize without any parameters being passed to it but I suspect it's Timer.

    'Randomize - Rem'med out, start at 0
    Randomize 0 'Start with 0
    Randomize 'Start with Timer (the time right now, I think)
    Randomize Timer 'Start with Timer
    Randomize Now 'Start with date and time
    Randomize 5 'Start at 5

    As you can see, it's very easy to recreate a sequence provided you know what you started with the first time.

    Now, for the accepted formula for Rnd:

    Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

    So if you want 0 to 10, you'd use:

    Int((10 - 0 + 1) * Rnd + 0)

    or

    Int((11) * Rnd)

    or

    Int(11 * Rnd)

    Rnd (in VB) actually creates a number between 0 and 1 (not including 1), this formula shifts it into the range we want.

    Hope this helps,
    KDL
  • : : : : Hello.
    : : : :
    : : : : I am a newbie to VB and have recently installed VB6. I used to do a little BASIC on my Commodore 64 but obviously that was quite some time ago! On that machine however it was easy to generate a random number, say between 0 and 10, with a simple line of code (though I can't remember what that was).
    : : : :
    : : : : What I am looking to do is generate a genuinely random number, say between 0 and 10. How would I make 'a' for instance, have a random value between 0 and 10?
    : : : :
    : : : : I have found the 'rnd' command but have read that the number generated would be repeated and so you use 'randomise' but to be honest I didn't really understand what it was all about. All I am looking to do is assign a genuine randomly generated value to 'a' for instance. Can you help me?
    : : : :
    : : : : Your assistance would be appreciated.
    : : : : Regards,
    : : : : Chris.
    : : : :
    : : : :
    : : : to use randomize you simply put it in the form load event of your form(you don't want it called more then once)
    : : :
    : : : then where ever you want your random number you would have something like the following:
    : : : dim intrand as integer
    : : : intrand = rnd * 11 ' should give you a random number between 0 and 10.
    : : :
    : : :
    : : : [blue]
    : : : C:Dos
    : : : C:Dos Run
    : : : Run Dos Run
    : : : [/blue]
    : : :
    : : :
    : : Thanks very much for that - I think I'm getting the hang of random numbers now. Just a couple of points:
    : :
    : : Dim intrand As Integer 'know what it does but what does 'Dim' mean?
    : :
    : : intrand = Rnd * 11 'actually gives you a random number between 0 and 11!
    : :
    : : But I found if you drop the 'Dim' statement and use this instead...
    : :
    : : intrand = Int(Rnd * 11)
    : :
    : : ...it WILL give you a number between 0 and 10 as you would expect. Strange heh? Give it a go and see what I mean.
    : :
    : : Also 'Randomize'. I've seen it on these pages as just 'Randomize', but also as 'Randomize Timer' and 'Randomize (Timer)'. Does adding 'Timer' change 'Randomize' in any way and do the brackets make a difference?
    : :
    : : Cheers,
    : : Chris.
    : :
    :
    : No such thing as a genuinely random number (though there are a few theories, they aren't available to us) so you need to fake it. This is done by using a formula that generates what appears to be a random number. This formula is called Rnd() in VB. Randomize allows you to change the formula's starting point so that a different sequence is used. If you don't use Randomize, it starts with 0. I forget what the default value is if you use Randomize without any parameters being passed to it but I suspect it's Timer.
    :
    : 'Randomize - Rem'med out, start at 0
    : Randomize 0 'Start with 0
    : Randomize 'Start with Timer (the time right now, I think)
    : Randomize Timer 'Start with Timer
    : Randomize Now 'Start with date and time
    : Randomize 5 'Start at 5
    :
    : As you can see, it's very easy to recreate a sequence provided you know what you started with the first time.
    :
    : Now, for the accepted formula for Rnd:
    :
    : Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    :
    : So if you want 0 to 10, you'd use:
    :
    : Int((10 - 0 + 1) * Rnd + 0)
    :
    : or
    :
    : Int((11) * Rnd)
    :
    : or
    :
    : Int(11 * Rnd)
    :
    : Rnd (in VB) actually creates a number between 0 and 1 (not including 1), this formula shifts it into the range we want.
    :
    : Hope this helps,
    : KDL
    :

    Thanks very much for that it was extremely helpful. I think I'm going to use 'Randomize Timer' in my programs. Just one more point, I know I only need to use 'Randomize' once, say in the form load event of my program, but what if I have several forms? Would I need to write it for each form? I'm guessing probably not, but I wanted to check.

    Thanks,
    Chris.
  • : Thanks very much for that it was extremely helpful. I think I'm going to use 'Randomize Timer' in my programs. Just one more point, I know I only need to use 'Randomize' once, say in the form load event of my program, but what if I have several forms? Would I need to write it for each form? I'm guessing probably not, but I wanted to check.
    :

    Nah, Randomize and Rnd (along with virtually everything else) are functions with "global" data so they will store the seed value from anywhere once it's given.

    Glad I could help!
  • I am getting in a complete pickle.

    I have 6 cases (trial types) and I want cases 1 and 4 to be selected 3 times each, and cases 2,3,5,6 to be selected once each.

    To select each case I use a random number generator to select a number which I transform into another one which references the case number.

    This is the code:

    [b]I have this amongst it:[/b]
    ******************************
    Dim intnumber As Integer
    Dim selnumber As Integer
    Dim orderchecker(1 To 10) As Integer
    *************************

    [b]
    then the randomise bit is:[/b]
    *********************************
    Randomize
    Do
    intnumber = Int(10 * Rnd)
    Loop Until orderchecker(intnumber) < 15
    orderchecker(intnumber) = orderchecker(intnumber) + 1
    If intnumber = 1 Then selnumber = 1
    If intnumber = 2 Then selnumber = 2
    If intnumber = 3 Then selnumber = 3
    If intnumber = 4 Then selnumber = 4

    If intnumber = 5 Then selnumber = 5
    If intnumber = 6 Then selnumber = 6
    If intnumber = 7 Then selnumber = 1
    If intnumber = 8 Then selnumber = 4
    If intnumber = 9 Then selnumber = 1
    If intnumber = 10 Then selnumber = 4


    Select Case selnumber
    ********************************************
    this keeps on going for 120 loops.

    If I want to generate numbers 1 to 10 then it works fine, no problems. However, I now want to select cases 1 and 4 FOUR times each instead of three. I changed
    intnumber = Int(10 * Rnd)
    to
    intnumber = Int(12 * Rnd)

    and it didn't work so I changed
    Dim orderchecker(1 To 10) As Integer
    to
    Dim orderchecker(1 To 12) As Integer

    and still it doesn't work. It does it for a while then it stops, highlights this line:

    Loop Until orderchecker(intnumber) < 15

    and says out "runtime error 9. out of range"

    Can anyone help? I could program it differently, but I am not a programmer really and this is doing my head in.

    Thanks in advance!!!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories