+ Reply to Thread
Results 1 to 7 of 7

Generating 365 Random numbers (0 to 5) that will sum to a specific value (1477)?

  1. #1
    Registered User
    Join Date
    02-19-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    3

    Generating 365 Random numbers (0 to 5) that will sum to a specific value (1477)?

    I'm a beginner in excel and would like to ask how do I generate 365 Random numbers (0 to 5) in Cell A1 to A365 that will sum to a specific value (1477)? And to be able to re-generate another set of random number when i hit F9? Appreciate the help! Randbetween doesnt seems to solve this problem.

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Generating 365 Random numbers (0 to 5) that will sum to a specific value (1477)?

    Try running this on a blank worksheet

    Please Login or Register  to view this content.
    多么想要告诉你 我好喜欢你

  3. #3
    Registered User
    Join Date
    02-19-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Generating 365 Random numbers (0 to 5) that will sum to a specific value (1477)?

    Thank you so much for the prompt reply! It works perfectly. I would like to generate another 365 Random numbers (0 to 5) on C1 to C365 that will sum to 913. I tried to add on to the code but couldnt get the final sum of 913. Is there something wrong with my coding? Thank you so much once again!


    Function rand(ByVal min As Double, Optional ByVal max As Double)
    Dim r As Double
    If max = 0 Then
    max = min
    min = 0
    End If
    r = Rnd
    rand = (r * 1000000) - ((max - min) * Int((r * 1000000) / (max - min))) + min
    End Function

    Sub EF977634_Rand()
    Dim i As Long
    i = 1
    Do
    Range("A1:A365").Formula = "=randbetween(0,5)"
    Range("B1").Formula = "=SUM(A1:A365)"
    ActiveSheet.Calculate
    i = i + 1
    Loop While Range("B1").Value <> 1477 And i <= 100 'do 100 attempts see if we get 1477

    With Range("A1:A365")
    .Value = .Value
    End With

    If Range("B1").Value = 1477 Then Exit Sub

    Do
    i = rand(1, 365)
    If Cells(i, 1).Value < 5 Then
    Cells(i, 1).Value = Cells(i, 1).Value + 1
    ActiveSheet.Calculate
    End If
    Loop While Range("B1").Value <> 1477 'find random number to increment until we get 1477

    Dim n As Long
    n = 1
    Do
    Range("C1:C365").Formula = "=randbetween(0,5)"
    Range("D1").Formula = "=SUM(C1:C365)"
    ActiveSheet.Calculate
    n = n + 1
    Loop While Range("D1").Value <> 913 And n <= 100 'do 100 attempts see if we get 913

    With Range("C1:C365")
    .Value = .Value
    End With

    If Range("D1").Value = 913 Then Exit Sub

    Do
    i = rand(1, 365)
    If Cells(n, 1).Value < 5 Then
    Cells(n, 1).Value = Cells(n, 1).Value + 1
    ActiveSheet.Calculate
    End If
    Loop While Range("D1").Value <> 913 'find random number to increment until we get 913

    End Sub

  4. #4
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Generating 365 Random numbers (0 to 5) that will sum to a specific value (1477)?

    You are welcome, but in the future please post code in
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-19-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Generating 365 Random numbers (0 to 5) that will sum to a specific value (1477)?

    Noted about the coding! Thank you so much for your help!

  6. #6
    Registered User
    Join Date
    02-18-2014
    Location
    new york
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Generating 365 Random numbers (0 to 5) that will sum to a specific value (1477)?

    It is great thing,but how can we use it for a range of numbers, like if we want to have more than one numbers e.g. 55, 60, 49 and we need random data to be generated between 1-5 against these numbers in one go.

  7. #7
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Generating 365 Random numbers (0 to 5) that will sum to a specific value (1477)?

    Hi onlineriaz, welcome.

    Please take some time to read the forum rules. Your post does not comply with the forum rules, in this case rule #2.

    You should start your own thread instead.

+ 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. Generating Random numbers that will sum to 1 specific value?
    By zr11 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-24-2019, 10:08 AM
  2. Generating a list of Random numbers of random length...
    By Growl in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-19-2013, 12:10 PM
  3. Excel 2007 : generating random numbers with exception
    By scbalaji in forum Excel General
    Replies: 2
    Last Post: 05-16-2012, 02:28 PM
  4. Generating a Random Number of Random Numbers
    By Garrus in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2010, 09:39 AM
  5. Generating Lots of Random Numbers
    By tpkcfa in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-10-2007, 03:57 PM

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