Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:59:52+00:00 2026-06-06T14:59:52+00:00

I’m trying to create a sampling tool for which I need to pick randomly

  • 0

I’m trying to create a sampling tool for which

I need to pick randomly generated numbers between 0 and 3000 in multiples of 500 such that their sum is a fixed number say 10000 in 12 slots

Over to that I need to run multiple iterations about 100000 to 1000000 (not decided yet.

My approach is to create a two dimensional array, put a constraint to validate the sum total of an iteration, and only if the condition is true enter it in the final data sample.

I’m making some very terrible mistakes with the following code could not figure out where… Help is appreciated

Public Sub Generatenums()
    Dim GRP() As Long, Random() As Long
    Dim RandomTotal As Long, t As Long, w As Long, i As Long, j As Long

    ReDim GRP(1 To 100, 1 To 12)
    ReDim Random(1 To 12)

    For i = 1 To 100
        For j = 1 To 12
            Random(j) = Int(Rnd() * 7) * 500
            RandomTotal = RandomTotal + Random(j)
        Next j

        If RandomTotal = 10000 Then
            For k = 1 To 12
                GRP(i, k) = Random(k)
            Next k
        End If
    Next i

    Range("A1").Select
    For t = 1 To 100
        For w = 1 To 12
            Cells(t, w).Value = GRP(t, w)
        Next w
    Next t
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:59:53+00:00Added an answer on June 6, 2026 at 2:59 pm

    Fantom, welcome to SO.

    The commentors are right: This will take a very long time to run.

    Instead of thinking of this as an array to fill as a certain sum, think of it as a Pachinko or Galton Board with 20 balls bouncing around and ending up in bins at the bottom. Why 20? Because you want a sum of 10000 in increments of 500, and consider 10000/500 = 20 as one ball.

    Now the program can run in linear time. You don’t randomize the sums, you randomize which of the 12 slots the ball will fall into. Then you multiply how many balls are in each slot by 500 and your sum will always be 10000.

    One of the loops looks like this:

    For i = 1 To 20
      x = Rnd() * 12 + 1
      If x > 12 Then x = 12
      If x < 1 Then x = 1
      bin(x) = bin(x) + 1
    Next i
    

    Is that enough to solve your problem, or do you need some more?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.