Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day, Please i need some help, this is what i want to achieve. I want to generate like 5 random numbers and have them stored in the database example
13456
12564
35565
34466
34566
23456
34677


C#
//I know that using this 
 static Random rand = new Random();
 lbl1.Text = Convert.ToString(rand.Next(10000, 99999));
gives me 23445
my concern now is how to generate those numbers like 30 at same time as i have explaned above and have them saved in the database...

thanks, I do appreciate your effort..
Posted
Comments
Praveen Kumar Upadhyay 23-Dec-14 7:07am    
So you want 30 random numbers having 5 digits??

C#
private const int NumberOfRandomInts = 30;

// initialize the Random generator outside any loop !
Random rnd = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);

List<int> rndIntList = new List<int>(NumberOfRandomInts );

while(rndIntList.Count < NumberOfRandomInts )
{
    int i = rnd.Next(10000, 99999);

    if (rndIntList.Contains(i)) continue;

    rndIntList.Add(i);
}</int></int>
Now store the content of 'rndIntList in your Database as required.
 
Share this answer
 
Check out below code, it will give you 30 random 5 digits numbers.

C#
List<int> numbers = new List<int>();

for (; numbers.Count < 30; )
{
    int i = new Random().Next(10000, 99999);
    if (numbers.Contains(i)) continue;

    numbers.Add(i);
}


if condition is been mentioned in the code, so that numbers should not contain duplicate numbers. You can change the for loop condition depend how many numbers you want.
 
Share this answer
 
v2
Comments
Maciej Los 23-Dec-14 7:17am    
+5
Praveen Kumar Upadhyay 23-Dec-14 7:21am    
Thanks a lot Maciej
Difameg Network Nigeria 23-Dec-14 7:24am    
thanks, Do i need to use Gridview to view them? how do i save the entire 30 numbers to the database
Praveen Kumar Upadhyay 23-Dec-14 7:26am    
You can use Gridview to view them, once it is stored into the database. To store into the database simply use insert query.
Difameg Network Nigeria 23-Dec-14 7:32am    
thanks, can you please show me how to, i have tried using this //Label1.Text = numbers.ToString();
just to see if the generated numbers can be visible but i am having this error code
System.Collections.Generic.List`1[System.Int32]
can you please show me how to insert them
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900