This generates a random number string. I needed to post this code to link to it :P
Random number string between 1 and 50
[code=c#]
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace daniweb
{
public partial class frmRandom : Form
{
public frmRandom()
{
InitializeComponent();
}
public static int GetRandom(int Maxvalue)
{
byte[] randomNumber = new byte[1];
System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
Gen.GetBytes(randomNumber);
int rand = Convert.ToInt32(randomNumber[0]);
return rand % Maxvalue + 1;
}
public static string GetRandomNumberString()
{
List<int> lst = new List<int>();
for (int i1 = 1; i1 <= 50; i1++)
lst.Add(i1);
StringBuilder result = new StringBuilder();
while (lst.Count > 0)
{
int rnd = lst[GetRandom(lst.Count)-1];
lst.Remove(rnd);
result.Append(rnd.ToString("F0"));
if (lst.Count > 0)
result.Append(" ");
}
return result.ToString(); ;
}
private void button1_Click(object sender, EventArgs e)
{
string rnd = GetRandomNumberString();
string[] sTest = rnd.Split(new char[] { ' ' });
if (sTest.Length != 50)
System.Diagnostics.Debugger.Break(); //Makes sure we have 50 elements
Console.WriteLine(rnd);
}
}
}
[/code]
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.