Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create an Array of Random Numbers whose sum is < X

Posted on 2010-11-20
6
Medium Priority
?
464 Views
Last Modified: 2012-05-10
How would I go about creating a 1 dimensional array with N elements,
whose sum < X?

thx
0
Comment
Question by:JustinW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 1

Author Comment

by:JustinW
ID: 34180813
and the random numbers are integers between
0 and Z
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 34180909
See attached code
public int[] GetRandomArray(int max, int sumMax, int size)
        {
            Debug.Assert(sumMax >= max, "Invalid usage");
            Debug.Assert(max > 0, "Invalid usage");
            Debug.Assert(size > 0, "Invalid usage");
            Random r = new Random();
            List<int> result = new List<int>(size);
            int sum = 0;
            for (int i = 0; i < size; i++)
            {
                result[i] = r.Next(max+1);
                sum = result[i] + sum;
            }
            while (sum > sumMax)
            {
                sum = 0;
                for (int i = 0; i < size; i++)
                {
                    if (result[i] > 0)
                        result[i] = result[i] > 0 ? (result[i] - 1): 0;
                }
            }
            return result.ToArray();
        }

Open in new window

0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 2000 total points
ID: 34180924
where maxSum = X
maxValue=Z
size=N
public int[] GetRandomArray(int maxValue, int maxSum, int size)
        {
            Debug.Assert(maxSum >= maxValue, "Invalid usage");
            Debug.Assert(maxValue > 0, "Invalid usage");
            Debug.Assert(size > 0, "Invalid usage");
            Random r = new Random();
            List<int> result = new List<int>(size);
            int sum = 0;
            for (int i = 0; i < size; i++)
            {
                result[i] = r.Next(0,maxValue);
                sum = result[i] + sum;
            }
            while (sum > maxSum)
            {
                sum = 0;
                for (int i = 0; i < size; i++)
                {
                    if (result[i] > 0)
                        result[i] = result[i] > 0 ? (result[i] - 1): 0;
                }
            }
            return result.ToArray();
        }

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Closing Comment

by:JustinW
ID: 34180934
very nice;
danke
0
 
LVL 1

Author Comment

by:JustinW
ID: 34180941
Here's my crack at it in ruby
def rnd_rng(leng, the_max)
  arr = Array.new(leng){0}
  the_max = rand(the_max)

  i = 0
  while the_max > 0 && i <leng do
    tmp = rand(the_max)
    arr[i] = tmp
    the_max = the_max - tmp
    i +=1
  end

  return arr.shuffle!
end

Open in new window

0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 34180943
and vb.net version is:
Public Function GetRandomArray(maxValue As Integer, maxSum As Integer, size As Integer) As Integer()
	Debug.Assert(maxSum >= maxValue, "Invalid usage")
	Debug.Assert(maxValue > 0, "Invalid usage")
	Debug.Assert(size > 0, "Invalid usage")
	Dim r As New Random()
	Dim result As New List(Of Integer)(size)
	Dim sum As Integer = 0
	For i As Integer = 0 To size - 1
		result(i) = r.[Next](0, maxValue)
		sum = result(i) + sum
	Next
	While sum > maxSum
		sum = 0
		For i As Integer = 0 To size - 1
			If result(i) > 0 Then
				result(i) = If(result(i) > 0, (result(i) - 1), 0)
			End If
		Next
	End While
	Return result.ToArray()
End Function

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question