How to share random numbers out evenly in C# - c#

I am looking at sharing out a fixed number of 32 teams between a varied number of people.
Of course, 32 may not always be evenly divisible, but for the sake of this exercise, lets say I am looking to share the 32 teams between 4 people, so a maximum number of 8 teams per person.
int max = 32 / numb;
foreach (string value in wcteams)
{
//Assigning teams to players
int selection = random.Next(0, numb);
int[] counter = new int[max];
counter[selection] = counter[selection] + 1;
if (counter[selection] < max)
{
Console.WriteLine(inputtedNames[selection] + " has drawn " + value);
}
}
Right now, I can run that code and I will get a list back of randomly chosen people along with their team. But the limit will not be implemented and some players will end up with more teams than others.
I understand that the following code:
counter[selection] = counter[selection] + 1;
Is not working to add up the number of teams that the user has received, am I on the right track here with how to tally up the number of times a player has been randomly selected or is there another method that I should be doing?

One problem in your code is you are initializing counter inside the loop. Also what happens if the count[selection] > max? you leave the team and don't assign it to anyone else.
Try the following code.
int numb = 4;
int max = 32 / numb;
int[] counter = new int[max];
foreach (string value in wcteams)
{
bool selectionComplete = false;
while(!selectionComplete)
{
int selection = random.Next(0, numb);
counter[selection] = counter[selection] + 1;
if (counter[selection] <= max)
{
selectionComplete = true;
Console.WriteLine(selection + " has drawn " + value);
}
}
}

I cannot figure your code but this should work.
public static Random randomT = new Random();
public static List<List<string>> DivideTeams(string[] teams, int personCount)
{
List<List<string>> divideTeams = new List<List<string>>();
if (teams.Length % personCount != 0)
{
throw new ArgumentOutOfRangeException();
}
//shuffle teams
for(int k = teams.Length -1; k > 0; k--)
{
int trade = random.Next(k + 1);
string temp = teams[trade];
teams[trade] = teams[k];
teams[k] = temp;
}
for (int j = 0; j < personCount; j++)
{
divideTeams.Add(new List<string>());
for (int i = 0; i < teams.Length / personCount; i++)
{
divideTeams[j].Add(teams[i]);
}
}
return divideTeams;
}

Related

Error from the 94th Fibonacci number in C#

I am calculating Fibonacci numbers in C# and I am getting wrong numbers since the number 94.
This is my code.
static void Main(string[] args)
{
string N = Console.ReadLine();
ulong n = ulong.Parse(N);
ulong[] fibonacci = new ulong[n+1];
fibonacci[0] = 0;
if (n == 1)
{
fibonacci[1] = 1;
}
if (n>=2)
{
fibonacci[1] = 1;
for (ulong i = 2; i < n+1; i++)
{
fibonacci[i] = (ulong)fibonacci[i-1] + (ulong)fibonacci[i-2];
}
}
}
Console.WriteLine(fibonacci[n]);
I am ok until the 93th number which is 12200160415121876738, but I am getting 1293530146158671551 with the 94th, the real one is 19740274219868223167.
I don't know what could be wrong with my code.
I think you don't need to store the number of N in the ulong type!
It can be stored as an int as well!
The most massive part is to store the value of the Nth Fibonacci number.
You can calculate it more concise without considering a large array for constructing the last number!
public static void Main(string[] args)
{
string N = Console.ReadLine();
int n = int.Parse(N);
BigInteger[] fibonacci = new BigInteger[3];
fibonacci[0] = 0;
fibonacci[1] = 1;
fibonacci[2] = 1;
if (n>=3)
{
for (int i = 3; i < n+1; i++)
{
fibonacci[0]=fibonacci[1];
fibonacci[1]=fibonacci[2];
fibonacci[2]=fibonacci[1]+fibonacci[0];
}
}
Console.WriteLine(fibonacci[2]);
}
The maximum value of ulong is 18,446,744,073,709,551,615.
You're trying to store 19,740,274,219,868,223,167, which is larger than that. By default a ulong won't throw an exception when it overflows, but you can force it to by adding a checked { } around the code:
checked
{
fibonacci[i] = (ulong)fibonacci[i-1] + (ulong)fibonacci[i-2];
}
Then running your code, sure enough, we get:
System.OverflowException: Arithmetic operation resulted in an overflow.
Fortunately, there is a type in .NET for working with really big integers. It's called BigInteger.
Changing your code to use it is relatively trivial:
string N = Console.ReadLine();
ulong n = ulong.Parse(N);
BigInteger[] fibonacci = new BigInteger[n+1];
fibonacci[0] = 0;
if (n == 1)
{
fibonacci[1] = 1;
}
if (n>=2)
{
fibonacci[1] = 1;
for (ulong i = 2; i < n+1; i++)
{
fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];
}
}
Your code now works as expected and outputs:
19740274219868223167
Even changing the input to 900 still works:
54877108839480000051413673948383714443800519309123592724494953427039811201064341234954387521525390615504949092187441218246679104731442473022013980160407007017175697317900483275246652938800
Try it online

Find N consecutive integers on a array, whose sum(of consecutive integers) S is a integer that i have

explanation
I have been staring at the problem for a few of minutes.
And i did some research before i ask this quest , but it were in different cases and they didn't included what i really need.
I found this piece of code in SO.
static int GetLargestSum(int[] array, int n, int sum)
{
int largestSum = 0;
int previousSum = 0;
for (int i = 0; i <= array.Length - n; i++)
{
if (i == 0)
{
for (int j = 0; j < n; j++)
{
largestSum += array[j];
}
previousSum = largestSum;
}
else
{
int currentSum = previousSum - array[i - 1] + array[i + n - 1];
if (currentSum > largestSum)
{
largestSum = currentSum;
}
previousSum = currentSum;
}
}
return largestSum;
}
And yes this works but if works for only the largest sum.
I tried to modify it to add the sum var into the code but that didn't actually went that well.
So i would really appreciate if someone helps me, bcs i am stuck in this algorithm.
Thank you!
The way to solve it would be to iterate over each segment of the array and evaluate its sum. A crude first draft would look something like this
public static int ConsecutiveSumArrangements(int[] vals, int count, int sum)
{
var number = 0;
for (int i = 0; i < (vals.Length - count); i++)
{
var segSum = vals.Skip(i).Take(count).Sum();
if (segSum == sum)
{
number++;
}
}
return number;
}
Maybe it is easier think in another way than try to correct this code. An idea is using slide window. Pseudo code look like
sum = 0
sol = 0
start = 0
end = 0
// sum of the first m elements
while end < m
sum = sum + s[end]
end = end + 1
If sum == d
sol = sol + 1
while end < n
sum = sum + s[end]
sum = sum - s[start]
end = end + 1
start = start + 1
if sum == d
sol = sol + 1
// in the loop we add the next element and subtract the first element
//so, we keep the length of m elements

C# - Read numbers from a textfile and output averages

I've looked at previous posts and none have the answer that I'm looking for.
I'm new to C# and trying to get this little application to work.
I have a file named "hours.txt" with 30 numbers in it and I want to read the file and output the Average and the Highest Number, but I can't seem to get it to work. I've tried changing the array to a string but I'm just really stuck on how to get this to work. Any help or tips would be greatly appreciated.
int[] hoursArray = new int[30];
StreamReader fileSR = new StreamReader("hours.txt");
int counter = 0;
string line = "";
line = fileSR.ReadLine();
while (line != null)
{
hoursArray[counter] = line;
counter = counter + 1;
line = fileSR.ReadLine();
}
fileSR.Close();
int total = 0;
double average = 0;
for (int index = 0; index < hoursArray.Length; index++)
{
total = total + hoursArray[index];
}
average = (double)total / hoursArray.Length;
int high = hoursArray[0];
for (int index = 1; index < hoursArray.Length; index++)
{
if (hoursArray[index] > high)
{
high = hoursArray[index];
}
}
Console.WriteLine("Highest number is: " + high);
Console.WriteLine("The average is: " + average);
Console.ReadLine();
This is wrong since your are putting a string into a int place.
hoursArray[counter] = line;
Parse string to int first.
hoursArray[counter] = int.Parse(line);
As an alternative method, using LINQ, the code could be replaced with the more concise:
string[] lines = System.IO.File.ReadAllLines("hours.txt");
int[] values = lines.Select((l) => int.Parse(l)).ToArray();
double average = values.Average();
int peak = values.Max();

Almost Ordered not sorting the exact amount of values i give it

this is a really easy question but i cant figure out a way around it. Apparently the almost ordered has a bug that it might randomize a little bit more than you ask it. the code is rather simple:
public void Section1Task1AlmostOrdered(int arraySize, int percentage)
{
int[] testArray = new int[arraySize];
Console.WriteLine("Ordered List: ");
for (int i = 1; i <= testArray.Length; i++)
{
testArray[i-1] = i;
Console.Write(i + "\t");
}
Console.WriteLine("Almost Ordered List: ");
testArray = shuffler.AlmostOrdered(arraySize, percentage);
for (int i = 0; i < testArray.Length; i++)
{
Console.Write(testArray[i] + "\t");
}
}
The shuffler is this part of the code:
public int[] AlmostOrdered(int n, double p)
{
if (p > 100)
{
throw new InvalidOperationException("Cannot shuffle more than 100% of the numbers");
}
int shuffled = 0;
//Create and Populate an array
int[] array = new int[n];
for(int i = 1; i <= n; i++)
{
array[i-1] = i;
}
//Calculate numbers to shuffle
int numsOutOfPlace = (int) Math.Ceiling(n * (p / 100));
int firstRandomIndex = 0;
int secondRandomIndex = 0;
do
{
firstRandomIndex = this.random.Next(n-1);
// to make sure that the two numbers are not the same
do
{
secondRandomIndex = this.random.Next(n - 1);
} while (firstRandomIndex == secondRandomIndex);
int temp = array[firstRandomIndex];
array[firstRandomIndex] = array[secondRandomIndex];
array[secondRandomIndex] = temp;
shuffled++;
}
while (shuffled < numsOutOfPlace);
return array;
}
When i enter values 10 for array size and 40 for percentage to be shuffled, it is shuffling 5 numbers instead of 4. Is there a way to perfect this method to make it more accurate?
Likely the problem is with the calculation:
int numsOutOfPlace = (int)Math.Ceiling(n * (p / 100));
So if p=40 and n=10, then in theory you should get 4. But you're dealing with floating point numbers. So if (p/100) returns 0.400000000001, then the result will be 4.000000001, and Math.Ceiling will round that up to 5.
You might want to replace Math.Ceiling with Math.Round and see how that works out.

Help with Creating a Recursive Function C#

I am creating a forecasting application that will run simulations for various "modes" that a production plant is able to run. The plant can run in one mode per day, so I am writing a function that will add up the different modes chosen each day that best maximize the plant’s output and best aligns with the sales forecast numbers provided. This data will be loaded into an array of mode objects that will then be used to calculate the forecast output of the plant.
I have created the functions to do this, however, I need to make them recursive so that I am able to handle any number (within reason) of modes and work days (which varies based on production needs). Listed below is my code using for loops to simulate what I want to do. Can someone point me in the right direction in order to create a recursive function to replace the need for multiple for loops?
Where the method GetNumbers4 would be when there were four modes, and GetNumbers5 would be 5 modes. Int start would be the number of work days.
private static void GetNumber4(int start)
{
int count = 0;
int count1 = 0;
for (int i = 0; 0 <= start; i++)
{
for (int j = 0; j <= i; j++)
{
for (int k = 0; k <= j; k++)
{
count++;
for (int l = 0; l <= i; l++)
{
count1 = l;
}
Console.WriteLine(start + " " + (count1 - j) + " " + (j - k) + " " + k);
count1 = 0;
}
}
start--;
}
Console.WriteLine(count);
}
private static void GetNumber5(int start)
{
int count = 0;
int count1 = 0;
for (int i = 0; 0 <= start; i++)
{
for (int j = 0; j <= i; j++)
{
for (int k = 0; k <= j; k++)
{
for (int l = 0; l <= k; l++)
{
count++;
for (int m = 0; m <= i; m++)
{
count1 = m;
}
Console.WriteLine(start + " " + (count1 - j) + " " + (j - k) + " " + (k - l) + " " + l);
count1 = 0;
}
}
}
start--;
}
Console.WriteLine(count);
}
EDITED:
I think that it would be more helpful if I gave an example of what I was trying to do. For example, if a plant could run in three modes "A", "B", "C" and there were three work days, then the code will return the following results.
3 0 0
2 1 0
2 0 0
1 2 0
1 1 1
1 0 2
0 3 0
0 2 1
0 1 2
0 0 3
The series of numbers represent the three modes A B C. I will load these results into a Modes object that has the corresponding production rates. Doing it this way allows me to shortcut creating a list of every possible combination; it instead gives me a frequency of occurrence.
Building on one of the solutions already offered, I would like to do something like this.
//Where Modes is a custom classs
private static Modes GetNumberRecur(int start, int numberOfModes)
{
if (start < 0)
{
return Modes;
}
//Do work here
GetNumberRecur(start - 1);
}
Thanks to everyone who have already provided input.
Calling GetNumber(5, x) should yield the same result as GetNumber5(x):
static void GetNumber(int num, int max) {
Console.WriteLine(GetNumber(num, max, ""));
}
static int GetNumber(int num, int max, string prefix) {
if (num < 2) {
Console.WriteLine(prefix + max);
return 1;
}
else {
int count = 0;
for (int i = max; i >= 0; i--)
count += GetNumber(num - 1, max - i, prefix + i + " ");
return count;
}
}
A recursive function just needs a terminating condition. In your case, that seems to be when start is less than 0:
private static void GetNumberRec(int start)
{
if(start < 0)
return;
// Do stuff
// Recurse
GetNumberRec(start-1);
}
I've refactored your example into this:
private static void GetNumber5(int start)
{
var count = 0;
for (var i = 0; i <= start; i++)
{
for (var j = 0; j <= i; j++)
{
for (var k = 0; k <= j; k++)
{
for (var l = 0; l <= k; l++)
{
count++;
Console.WriteLine(
(start - i) + " " +
(i - j) + " " +
(j - k) + " " +
(k - l) + " " +
l);
}
}
}
}
Console.WriteLine(count);
}
Please verify this is correct.
A recursive version should then look like this:
public static void GetNumber(int start, int depth)
{
var count = GetNumber(start, depth, new Stack<int>());
Console.WriteLine(count);
}
private static int GetNumber(int start, int depth, Stack<int> counters)
{
if (depth == 0)
{
Console.WriteLine(FormatCounters(counters));
return 1;
}
else
{
var count = 0;
for (int i = 0; i <= start; i++)
{
counters.Push(i);
count += GetNumber(i, depth - 1, counters);
counters.Pop();
}
return count;
}
}
FormatCounters is left as an exercise to the reader ;)
I previously offered a simple C# recursive function here.
The top-most function ends up having a copy of every permutation, so it should be easily adapted for your needs..
I realize that everyone's beaten me to the punch at this point, but here's a dumb Java algorithm (pretty close to C# syntactically that you can try out).
import java.util.ArrayList;
import java.util.List;
/**
* The operational complexity of this is pretty poor and I'm sure you'll be able to optimize
* it, but here's something to get you started at least.
*/
public class Recurse
{
/**
* Base method to set up your recursion and get it started
*
* #param start The total number that digits from all the days will sum up to
* #param days The number of days to split the "start" value across (e.g. 5 days equals
* 5 columns of output)
*/
private static void getNumber(int start,int days)
{
//start recursing
printOrderings(start,days,new ArrayList<Integer>(start));
}
/**
* So this is a pretty dumb recursion. I stole code from a string permutation algorithm that I wrote awhile back. So the
* basic idea to begin with was if you had the string "abc", you wanted to print out all the possible permutations of doing that
* ("abc","acb","bac","bca","cab","cba"). So you could view your problem in a similar fashion...if "start" is equal to "5" and
* days is equal to "4" then that means you're looking for all the possible permutations of (0,1,2,3,4,5) that fit into 4 columns. You have
* the extra restriction that when you find a permutation that works, the digits in the permutation must add up to "start" (so for instance
* [0,0,3,2] is cool, but [0,1,3,3] is not). You can begin to see why this is a dumb algorithm because it currently just considers all
* available permutations and keeps the ones that add up to "start". If you want to optimize it more, you could keep a running "sum" of
* the current contents of the list and either break your loop when it's greater than "start".
*
* Essentially the way you get all the permutations is to have the recursion choose a new digit at each level until you have a full
* string (or a value for each "day" in your case). It's just like nesting for loops, but the for loop actually only gets written
* once because the nesting is done by each subsequent call to the recursive function.
*
* #param start The total number that digits from all the days will sum up to
* #param days The number of days to split the "start" value across (e.g. 5 days equals
* 5 columns of output)
* #param chosen The current permutation at any point in time, may contain between 0 and "days" numbers.
*/
private static void printOrderings(int start,int days,List<Integer> chosen)
{
if(chosen.size() == days)
{
int sum = 0;
for(Integer i : chosen)
{
sum += i.intValue();
}
if(sum == start)
{
System.out.println(chosen.toString());
}
return;
}
else if(chosen.size() < days)
{
for(int i=0; i < start; i++)
{
if(chosen.size() >= days)
{
break;
}
List<Integer> newChosen = new ArrayList<Integer>(chosen);
newChosen.add(i);
printOrderings(start,days,newChosen);
}
}
}
public static void main(final String[] args)
{
//your equivalent of GetNumber4(5)
getNumber(5,4);
//your equivalent of GetNumber5(5)
getNumber(5,5);
}
}

Categories

Resources