Results 1 to 6 of 6

Thread: I need help!

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    3

    I need help!

    How do i make numbers randomize when i want them to be between 1 and 10 only?



    Please..i need help

  2. #2
    Addicted Member TheAlchemist's Avatar
    Join Date
    Jan 2003
    Location
    Dar-esSalaam,Tanzania
    Posts
    139
    hey mate,
    the Rnd() function returns pseudo random numbers between 0 and 1 inclusive. to extend this range to between 1 and 10 just do this:

    VB Code:
    1. Dim RetVal As Single
    2.  RetVal = Rnd * 10 ' this will produce numbers between 0 and 10

    all you have to do now is to test whether the produced number is less than 1 and redo the line if it is.
    One thing that sustains me through life is the conciousness of the immense inferiority of everyone else
    --Oscar Wilde

  3. #3
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    Hmm, I always thought that the Rnd function was 0 inclusive, but 1 exclusive.

    Furthermore there's no need to delete numbers less than 0, with the following calculation:
    VB Code:
    1. RetVal = Rnd * 9 + 1

    The difference between 10 and 1 is 9. The minimum number is 1.
    This function will return floating point values.

    If you want to have a integer value between 1 and 10 (both inclusive) then you can use this calculation:
    VB Code:
    1. RetVal = Int(Rnd * 10) + 1

  4. #4
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking One more thing

    If you want 'truly' random numbers (at least, closer to truly random) then chuck this before your call:
    VB Code:
    1. Randomize Timer
    2. 'Picking the random number 1 to n:
    3. rand_int= Int(Rnd*n)+1
    sql_lall

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    3
    ok
    but i want it the random number to be displayed on labels called

    lblnumber1 'and
    lblnumber2


    how do i make it display the random numbers there when the form loads?

  6. #6
    Addicted Member TheAlchemist's Avatar
    Join Date
    Jan 2003
    Location
    Dar-esSalaam,Tanzania
    Posts
    139
    hey guys,

    Riis you're right. My mistake. The rnd function produces the following set : 0 =< Rnd < 1

    it is not inclusive of 1.

    According to the Visual Basic Documentation, to produce a pseudo random number between an upper bound and lower bound, inclusive, use the following formula:

    VB Code:
    1. Int((UpperBound - LowerBound +1 ) * Rnd) +1
    One thing that sustains me through life is the conciousness of the immense inferiority of everyone else
    --Oscar Wilde

Posting Permissions

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



Click Here to Expand Forum to Full Width