Reputation: 840
I am working on a small assignment where I need to display data (0 to 99 - make sure all the numbers are displayed) randomly in a data grid view of 10 rows and 10 columns. I am unable to figure out the logic that needs to be written for this scenario. Currently I am doing like shown below, two for loops, one for row and other columns and iterating it...
int[] arr = new int[] { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 58, 69, 60, 61, 62, 63, 64 };
char c = Convert.ToChar(arr.GetValue(new Random().Next(0, 21)));
for (int row = 0; row < 10; row++)
{
for (int col = 0; col < 10; col++)
{
if ((row + col) == 9)
{
this.dataGridView1.Rows[row].Cells[col].Value = string.Format("{0} {1}", c, ((row + 1) * 9));
}
else
{
this.dataGridView1.Rows[row].Cells[col].Value = string.Format("{0} {1}", Convert.ToChar(arr.GetValue(new Random().Next(0, 21))), row * 10 + (col));
System.Threading.Thread.Sleep(15);
}
}
}
Out put:
Expected:
Code for randomly distributing the numbers from 0 to 99.
Thanks in Advance.
Upvotes: 1
Views: 2178
Reputation: 840
This is what I was finally able to do it. It worked!!!
public static class ThreadSafeRandom
{
[ThreadStatic]
private static Random Local;
public static Random ThisThreadsRandom
{
get { return Local ?? (Local = new Random(unchecked(Environment.TickCount * 31 + Thread.CurrentThread.ManagedThreadId))); }
}
}
static class MyExtensions
{
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = ThreadSafeRandom.ThisThreadsRandom.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
The above code does the shuffling of numbers.
var numbers = new List<int>(Enumerable.Range(0, 100));
numbers.Shuffle();
List<int> final = numbers.GetRange(0, 100);
int[,] f = new int[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
f[i, j] = final[(i * 10) + j];
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (f[i, j] % 9 == 0)
{
this.dataGridView1.Rows[i].Cells[j].Value = string.Format("{0} {1}", charSymbol, f[i, j]);
}
else
{
this.dataGridView1.Rows[i].Cells[j].Value = string.Format("{0} {1}", GetRandomChar(), f[i, j]);
}
}
}
Upvotes: 0
Reputation: 790
Check with this if you ain't done with your coding...
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
for (int i = 0; i < 10; i++)
dt.Columns.Add("col" + i.ToString(), typeof(int));
Random r = new Random();
List<int> l = new List<int>(100);
int temp=0;
for (int i = 9; i >= 0; i--)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < 10; j++)
{
temp = r.Next(99);
if (!l.Contains(temp) || (j == 0 && i == 0))
{
dr["col" + j.ToString()] = temp;
l.Add(temp);
}
else
j -= 1;
}
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
Upvotes: 0
Reputation: 5538
By small assignment do you mean school assignment? If so, I don't want to write everything for you, but to point you in the right direction:
The first step I would take initializing an array with a size of 100, and placing the numbers 0-99 on the array. Then, I would find a method for shuffling the array (Linq's OrderBy used with the Random class may be useful to you here). Then, I would iterate through the shuffled array, and place those items inside the grid. I think you have the general grid looping idea down, with the embedded for loops... I'm not sure what the point of the Thread Sleep is, though.
Upvotes: 2