+ Reply to Thread
Results 1 to 13 of 13

generate random numbers between 1 to 10 vba rnd()

  1. #1
    Registered User
    Join Date
    04-08-2014
    Location
    München, Deutschland
    MS-Off Ver
    Excel 2013
    Posts
    7

    generate random numbers between 1 to 10 vba rnd()

    Dear all,

    I have a quick question and i would be very thankful if someone could help me.

    I would like to generate randomly ten numbers in a row between 1 and 10.

    For instance: 9,5,2,5,6,8,1,4,3, etc.

    Please Login or Register  to view this content.
    the above code give me numbers between 1 and 100, any idea on how I could limit the possible outcomes between 1 and 10

    Thanks in advance for your time.

    -Thomas
    Last edited by ihrktho; 04-08-2014 at 09:10 AM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: generate random numbers between 1 to 10 vba rnd()

    Try this:
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: generate random numbers between 1 to 10 vba rnd()

    Maybe:

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    04-08-2014
    Location
    München, Deutschland
    MS-Off Ver
    Excel 2013
    Posts
    7

    Re: generate random numbers between 1 to 10 vba rnd()

    Thanks for your quick answer.

    Your solution work well for rnd number between 1 and 10.

    but would you know how to generate rnd number between 1 and 12?

  5. #5
    Registered User
    Join Date
    04-08-2014
    Location
    München, Deutschland
    MS-Off Ver
    Excel 2013
    Posts
    7

    Re: generate random numbers between 1 to 10 vba rnd()

    Thanks John H Davis,

    Your solution also works perfectly.

    but I was also wondering how to do, if I am looking at random number between 1 and another number that is not a multiple of 10. for example number between 1 and 12.

    Thanks again for your quick answer.

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: generate random numbers between 1 to 10 vba rnd()

    perhaps take a look at the vba help on it's rnd function. It provides an example of getting a random number between to values.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: generate random numbers between 1 to 10 vba rnd()

    I tested this and it appears to work.

    Please Login or Register  to view this content.

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: generate random numbers between 1 to 10 vba rnd()

    The use of this line to handle zero value will skew the output. Even if 10 was changed to 12 for this specific case.

    Please Login or Register  to view this content.
    Whether that's inmportant to the OP I don't know.

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: generate random numbers between 1 to 10 vba rnd()

    Thanks for pointing that out.

  10. #10
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: generate random numbers between 1 to 10 vba rnd()

    x = WorksheetFunction.RandBetween(1, 12)
    Ben Van Johnson

  11. #11
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: generate random numbers between 1 to 10 vba rnd()

    PHP Code: 
    Sub VietNam()
    Dim X As Long
    Dim RndnumberArray
    (1 To 10) As Integer

    For 1 To 10
        RndnumberArray
    (X) = CInt(Rnd()) + 10 X
        MsgBox RndnumberArray
    (X)
    Next X
    End Sub 

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: generate random numbers between 1 to 10 vba rnd()

    @Sa DQ,

    Did you test your code?

    This is the output I got from running it.

    11
    21
    32
    41
    52
    65
    70
    86
    97
    107

  13. #13
    Registered User
    Join Date
    04-08-2014
    Location
    München, Deutschland
    MS-Off Ver
    Excel 2013
    Posts
    7

    Re: generate random numbers between 1 to 10 vba rnd()

    Thanks so much,

    thanks to your help I understand that:
    1) I could use the worksheet function: RandBetween.
    2) Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    3) RndnumberArray(X) = CInt(X * Rnd()) + 10 * X

    Thanks again for your help,
    Thomas.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Generate Random Numbers Between 0-9
    By dreicer_Jarr in forum Excel General
    Replies: 11
    Last Post: 01-17-2014, 01:00 AM
  2. [SOLVED] Generate Random numbers where sum = 1
    By sajeel in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-18-2013, 07:12 AM
  3. [SOLVED] Generate Random Numbers
    By stojko89 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-09-2013, 02:49 AM
  4. Using VBA to generate random numbers
    By matt3542 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-02-2008, 09:15 AM
  5. Generate Random Numbers
    By Rgaherty in forum Excel General
    Replies: 5
    Last Post: 10-30-2007, 04:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1