0 and Z
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();
}
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();
}
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
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
Title | # Comments | Views | Activity |
---|---|---|---|
Code enhancement | 5 | 12 | |
Enable copy and pasting even when textbox is disabled | 2 | 14 | |
Format column on datatable | 7 | 13 | |
Bypass cross origin issues on development site | 3 | 17 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
14 Experts available now in Live!