When I try to generate a new random int each time to fill the array it just gives me back the same number. The seed won't change. I'm sure there is an easy answer but I have been looking at it for a few hours and just can't see it.
internal class LingoCard : ILingoCard
{
private ICardNumber[,] _cardNumber;
private int _teller;
private int _oddTeller;
private int rand;
Random rnd = new Random();
public LingoCard(bool useEvenNumbers)
{
ICardNumber[,] cardNumber = new ICardNumber[5,5];
_cardNumber = cardNumber;
if (useEvenNumbers == true)
{
UseEvenNumbers(_cardNumber);
} else {
UseOddNumbers(_cardNumber);
}
}
public void UseEvenNumbers(ICardNumber[,] cardNumber)
{
while (_teller < 70)
{
for (int g = 0; g < 1; g++)
{
rand = rnd.Next(0, 70);
CardNumber _walue = new CardNumber(rand);
if (rand % 2 == 0)
{
for (int i = 0; i < cardNumber.GetLength(0); i++)
{
for (int j = 0; j < cardNumber.GetLength(1); j++)
{
_cardNumber[i, j] = _walue;
}
_teller++;
}
}
}
}
}
The reason your random number is the same for all the card numbers is because you're generating one Random number before the two nested for loops and using it to fill in every card number
rand = rnd.Next(0, 70); // Random number generated once here
CardNumber _walue = new CardNumber(rand);
if (rand % 2 == 0)
{
for (int i = 0; i < cardNumber.GetLength(0); i++)
{
for (int j = 0; j < cardNumber.GetLength(1); j++)
{
_cardNumber[i, j] = _walue; // same random value assigned for every card number here
}
_teller++;
}
}
In order to fix it you'd need to generate the random number inside the for loop when you are assigning _cardNumber
for (int i = 0; i < cardNumber.GetLength(0); i++)
{
for (int j = 0; j < cardNumber.GetLength(1); j++)
{
rand = rnd.Next(0, 70) * 2; // Multiply by 2 so the random number is even
CardNumber _walue = new CardNumber(rand);
_cardNumber[i, j] = _walue;
}
_teller++;
}
Let me know if this helps!
Related
i need to write a program that takes 1 to 100 integers randomly, then i need to take this program's transpose, then I need to sort this matrix (each row in itself) from smallest to largest.
and finally, i need to find this matrix's peak value. here is what i have written for the program. for now, it creates a random matrix (20x5) and then it takes its transpose. can you help me with finding its peak value and then sort its each row?
PS.: using classes is mandatory!
using System;
namespace my_matrix;
class Program
{
public int[,] Create(int[,] myarray, int Row, int Clm)
{
Random value = new Random();
myarray = new int[Row, Clm];
int i = 0;
int j = 0;
while (i < Row)
{
while (j < Clm)
{
myarray[i, j] = value.Next(1, 100);
j++;
}
i++;
j = 0;
}
return myarray;
}
public int[,] Print(int[,] myarray, int Row, int Clm)
{
Console.WriteLine("=====ARRAY=====");
for (int a = 0; a < Row; a++)
{
for (int b = 0; b < Clm; b++)
{
Console.Write(myarray[a, b] + " ");
}
Console.WriteLine();
}
return null;
}
public int[,] Transpose(int[,] myarray, int Row, int Clm)
{
for (int b = 0; b < Clm; b++)
{
for (int a = 0; a < Row; a++)
{
Console.Write(myarray[a, b] + " ");
}
Console.WriteLine();
}
return myarray;
}
public int[,] Print_Transpose(int[,] myarray, int Row, int Clm)
{
Console.WriteLine("=====TRANSPOSE=====");
for (int b = 0; b < Clm; b++)
{
for (int a = 0; a < Row; a++)
{
Console.Write(myarray[a, b] + " ");
}
Console.WriteLine();
}
return null;
}
static void Main(string[] args)
{
Program x = new Program();
int[,] myarray = new int[20, 5];
int[,] a = x.Create(myarray, 20, 5);
x.Print(a, 20, 5);
x.Print_Transpose(a, 20, 5);
}
}
i don't know how to make a class which sorta each row from smallest to largest and also i dont know how to create a class which finds the matrix's peak value.
You can run over the array, keeping the maxNum:
public int getMax(int[,] fromArray)
{
int maxNum = 0;
for (int b = 0; b < fromArray.GetUpperBound(1); b++)
{
for (int a = 0; a < fromArray.GetUpperBound(0); a++)
{
if (maxNum < fromArray[a,b])
{
maxNum = fromArray[a, b];
}
}
}
return maxNum;
}
Call the function like:
Console.Write("Max number = {0:D}", x.getMax (a));
If you want a sort method, you can place all values in a List, sort them and convert back to array.
public int[] SortArray(int[,] fromArray)
{
List<int> newList = new List<int>(fromArray.Length);
for (int b = 0; b < fromArray.GetUpperBound(1); b++)
{
for (int a = 0; a < fromArray.GetUpperBound(0); a++)
{
newList.Add (fromArray[a, b]);
}
}
newList.Sort();
return newList.ToArray<int>();
}
I need to streamwrite into a file.txt a 10 random numbers combinations (NOT REPEATED) like lottery program. I got everything except Non-repeated random numbers. it has to be seen (file.txt) like a 2D array with 10 combinations thx.
class Matriz
{
private int[,] array;
private int nfilas, ncols;
public void Ingresar()
{
Random aleatori = new Random();
nfilas = 10;
ncols = 6;
Console.WriteLine("\n");
array = new int[nfilas, ncols];
for (int filas = 0; filas < nfilas; filas++)
{
for (int columnas = 0; columnas < ncols; columnas++)
array[filas, columnas] = aleatori.Next(0, 50);
}
}
public void Imprimir()
{
StreamWriter fitxer = new StreamWriter(#"C:\andres\lotto649.txt");
int contador = 0;
for (int f = 0; f < nfilas; f++)
{
for (int c = 0; c < ncols; c++)
fitxer.Write(array[f, c] + " ");
fitxer.WriteLine();
contador++;
}
fitxer.WriteLine($"\n\n\tHay {contador} combinaciones de la Loteria 6/49");
fitxer.Close();
}
static void Main(string[] args)
{
Matriz array_menu = new Matriz();
array_menu.Ingresar();
array_menu.Imprimir();
}
}
Something like this should work.
Before inserting the new number in the given position, check the row thus far if the same number is already in there and if so, repeat the process until you get a number that isn't yet in there.
for (int filas = 0; filas < nfilas; filas++)
{
for (int columnas = 0; columnas < ncols; columnas++)
{
bool cont = true;
while(cont)
{
cont = false;
int newRand = aleatori.Next(0, 50);
for(int i = 0; i < columnas; i++)
if(array[filas, i] == newRand)
cont = true;
if(cont)
continue;
array[filas, columnas] = newRand;
break;
}
}
}
Alternatively, since the number is small you could also work with a list of ints and remove the given value from there.
This has the advantage, that you'll never have to redo your random number as it will always produce a valid result. The above example could (theoretically) run indefinitely long.
for (int filas = 0; filas < nfilas; filas++)
{
List<int> nums = new List<int>(49);
for(int i = 0; i < 49; i++)
nums.Add(i + 1); //num 1...49
for (int columnas = 0; columnas < ncols; columnas++)
{
int index = aleatori.Next(0, nums.Count)
array[filas, columnas] = nums[index];
nums.RemoveAt(index);
}
}
I have a double[,] and I want to create a list of each row of this double[,] and then create a list of this lists. I tried this code but get an exception out of range for: lQC[j].Add(new double());
double[,] disQC = new double[42, TTools.Depth.Count];
List<double> mQC = new List<double>();
mQC.Add(new double());
for (int j = 0; j < Example.Count; j++)
{
mQC.Add(100);
for (int i = 0; i < 42; i++)
{
if (XQuartz[i] < TPorosity.Neutron[j] && TPorosity.Neutron[j] < XCalcite[i] && YQuartz[i] < TPorosity.BulkDensity[j] && TPorosity.BulkDensity[j] < YCalcite[i])
{
mQC[i] = (YCalcite[i] - YQuartz[i]) / (XCalcite[i] - XQuartz[i]);
disQC[i, j] = (Math.Abs((TPorosity.BulkDensity[j] - YQuartz[i] - (mQC[i] * TPorosity.Neutron[j]) + (mQC[i] * XQuartz[i])) / Math.Sqrt(Math.Pow(mQC[i], 2) + 1)));
}
else
{
disQC[i, j] = 100;
}
List<List<double>> lQC = new List<List<double>>();
lQC.Add(new List<double>());
lQC[j].Add(new double());
lQC[j].Add(disQC[i, j]);
List<int> MinimumIndexQC = new List<int>();
MinimumIndexQC.Add(80000);
MinimumIndexQC[j] = lQC[j].IndexOf(lQC[j].Min());
}
}
Hope anyone can help me!
At every iteration of the for statement you create a new lQC. Move it before the for.
Here is the correct solution. Demo
public class Program {
public static void Main()
{
double[,] disQC = new double[5, 5];
List<List<double>> lQC = new List<List<double>>();
for (int j = 0; j < 5; j++) {
lQC.Add(new List<double>());
for (int i = 0; i < 5; i++) {
if (i % 2 == 0){
disQC[i, j] =i + 1 ;
}
else{
disQC[i, j] = j + 1;
}
lQC[j].Add(disQC[i, j]);
}
}
lQC.ForEach(l => { l.ForEach(t => Console.Write(t)); Console.WriteLine(""); });
}
}
I don't know how to shuffle 2D array without duplicate elements. Can anyone help me to shuffle a 2D array?
Here is what I have so far:
public class Shuffle2DArray
{
public Shuffle2DArray ()
{
}
public static void Main(string[] args)
{
int[,] a = new int[3, 3] { { 1, 2, 3, }, { 6, 7, 8 }, { 11, 12, 13 } };
Shuffle2DArray shuffle = new Shuffle2DArray ();
shuffle.getshuffle2D (a);
}
void getshuffle2D(int[,] arr)
{
Random ran = new Random ();
for (int i = 0; i < arr.GetLength (0); i++) {
for (int j = 0; j < arr.GetLength (1); j++) {
int m = ran.Next(arr.GetLength (0)-1);
int n = ran.Next(arr.GetLength (1)-1);
int temp = arr[0,j];
arr[i,0] = arr[m,n+1];
arr[m,n] = temp;
Console.Write(arr[i,j]+ "\t");
}
Console.WriteLine();
}
}
}
Well, I would say shuffle the 2d array the same way as you shuffle the 1d array.
For instance, Fisher–Yates shuffle for 1d array is something like this
public static class Utils
{
public static void Swap<T>(ref T a, ref T b) { var temp = a; a = b; b = temp; }
public static void RandomShuffle<T>(this T[] target, Random random = null)
{
if (target.Length < 2) return;
if (random == null) random = new Random();
for (int i = target.Length - 1; i > 0; i--)
{
int j = random.Next(i + 1);
if (i != j) Swap(ref target[i], ref target[j]);
}
}
}
All you need is to realize that having a 2d array
T[,] array
and accessing the element of the array
array[row, column]
that
row = index / columnCount
column = index % columnCount
where
index = [0, array.Lenght - 1] corresponds to the index in 1d array
columnCount = array.GetLength(1)
adding the 2d version function to the class above is trivial
public static class Utils
{
// ...
public static void RandomShuffle<T>(this T[,] target, Random random = null)
{
if (target.Length < 2) return;
if (random == null) random = new Random();
int columnCount = target.GetLength(1);
for (int i = target.Length - 1; i > 0; i--)
{
int j = random.Next(i + 1);
if (i != j) Swap(ref target[i / columnCount, i % columnCount], ref target[j / columnCount, j % columnCount]);
}
}
}
Sample usage:
int[,] a = new int[3, 3] { { 1, 2, 3, }, { 6, 7, 8 }, { 11, 12, 13 } };
a.RandomShuffle();
You need to first order your array by random sequence of numbers. What you are doing now is just changing two random items of 2d array at each iterate thus it may result in duplicate items.
Look at this Sort algorithm for 1d array.
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] > arr[j]) // ran.Next(-1,1) == 0 // or any random condition
{
int temp = arr[i];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
As you can see we need 2 loops to sort 1d array. So in order to sort 2d array we need 4 loops.
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(0); j++)
{
for (int k = 0; k < arr.GetLength(1); k++)
{
for (int l = 0; l < arr.GetLength(1); l++)
{
if (arr[i, k] > arr[j, l]) // ran.Next(-1,1) == 0
{
int temp = arr[i, k];
arr[i, k] = arr[j, l];
arr[j, l] = temp;
}
}
}
}
}
Then write another algorithm to print items.
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + "\t");
}
Console.WriteLine();
}
This was Sort algorithm. Now if you just change this condition with random one you sort your array by random.
Change if (arr[i, k] > arr[j, l])
To if (ran.Next(-1,1) == 0). this is just randomly true or false.
Im writing a program where the user writes how many times he want to throw x-number dice and how many sides they going to have.
But i cant figure out how to return the sum om the number on each dice game..
this is the main code:
static void Main(string[] args)
{
List<Dice> _Dice = new List<Dice>();
int a = 0;
int ggr = int.Parse(Interaction.InputBox("How many times do you want to repeat:"));
while (a != ggr)
{
int xChoice = int.Parse(Interaction.InputBox("How many dice do you want to throw:"));
int yChoice = int.Parse(Interaction.InputBox("Write how many sides the dice will have:"));
_Dice.Add(new Dice(xChoice,yChoice));
a++;
}
int e = 1;
foreach (var item in _Dice)
{
Interaction.MsgBox(string.Format("Result off game {0}: {1}", e++, item.ToString()));
}
}
This is the Dice class:
static int _xChoice, _yChoice;
static int[,] dice = new int[_xChoice, _yChoice];
public int Tostring()
{
int a = 0;
foreach (var item in dice)
{
a+=item;
}
return a;
}
void throw()
{
Random r = new Random();
for (int i = 0; i <dice.GetLength(0); i++)
{
for (int j = 0; j < dice.GetLength(1); j++)
{
dice[i, j] = r.Next(1, _yChoice);
}
}
}
public Dice(int Xchoice, int Ychoice)
{
_xChoice = Xchoice;
_yChoice = Ychoice;
}
Just for completeness, what you are asking for is the sum of items of a 2D array:
int total = Enumerable.Range(0, _xChoice).Sum(s => Enumerable.Range(0, _yChoice).Sum(p => dice[s, p]));
Well you can do it like this:
void throw()
{
Random r = new Random();
for (int i = 0; i <dice.GetLength(0); i++)
{
int totalSum = 0;
for (int j = 0; j < dice.GetLength(1); j++)
{
dice[i, j] = r.Next(1, _yChoice);
totalSum += dice[i, j];
}
// Here you display totalSum for game with index i.
}
}