Thursday, April 8, 2010

Generate random numbers who's sum is a pre-determined number in C#

Here we have gererated a 9 digit random number whose sum is multiple of 9
and starting no will be 6 and 7

public void GetUnique()
{
string b=Getval();
char[] a=b.tochararray()

int b1=0;
foreach(char v in a)
{
b1+=Convert.ToInt32(v);
}

if (b1%9==0)
{
print value;
}
else
GetUnique() ;

}

public string Getval()
{
Random r = new Random();
string b = String.Format
(
"{0}{1}{2}{3}{4}{5}{6}{7}{8}",
r.Next(6, 7),
r.Next(0, 9),
r.Next(0, 9),
r.Next(0, 9),
r.Next(0, 9),
r.Next(0, 9),
r.Next(0, 9),
r.Next(0, 9),
r.Next(0, 9)
);
return b;

}

No comments:

Post a Comment

Mat Table Angular

Mat Table in Angular Mat table is used to display data . its a material designed styled data-table . For using Material Table MatTableMo...