Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi
i want create a random number between (min,max)values that read of my database and select a this number that saved in database .
my program is for draw and i do not have repeat number . anybody can help me??
Posted
Comments
Sandeep Mewara 15-May-11 9:47am    
Sure someone can but you need to start first. Try, share the effort and ask for help.
Gregory Gadow 17-May-11 0:23am    
Trick question: if you are eliminating numbers that have already been selected, then by definition you are not selecting random numbers.

Something like what this guy wanted last week?

How to check a random value and avoid for the same random value?[^]
 
Share this answer
 
If you need an algorithm for avoiding repetition of random numbers, then may have a look at my tip: "Random extraction of 5 cards from a deck"[^] (it's C++ code but heavily commented).
:-)
 
Share this answer
 
Follow the steps.

Step 1. Create method VerifyInDB(string/int randomvariable) in you cs file as follows
 public bool VerifyInDB(string/int RandomVariable)
{
bool b_RandomStatus=false;
sqlconnection con_SQLConnectionString=new sqlConnection(Constr);
SqlDataAdapter da_ForCheckforRandomInDB=new  SqlDataAdapter("select * from DB..tbl_table where RandomColumnName="+ randomvariable, con_SQLConnectionString);

DataTable dt_ForCheckforRandomInDB=new DataTable()
da.fill(dt_ForCheckforRandomInDB)
if(dt_ForCheckforRandomInDB.Rows.Count()>0)
	{
		b_RandomStatus=true;

	}
return b_RandomStatus;

}


Step 2. Create another method in the same *.cs file as follows

 public string/int CreateRandomNumber()
{
        //Assign random value here
	string/int RandomVariable ;//=Random();
	if(VerifyInDB(RandomVariable))
		{
                  //Random value Present in DB Call CreateRandomNumber() again
			CreateRandomNumber()
		}	

Return RandomVariable;

}

Step 3: Call CreateRandomNumber() any where in your code this will return random value which is not present in your DataBase.

Step 4:int/String RandumValue=CreateRandomNumber(); you will get the random value which is not repeated in DataBase.
 
Share this answer
 
v6
 
Share this answer
 
Comments
mehdi smm 15-May-11 9:37am    
i want get without repeat random
C#
Random rnd = new Random();
protected int GetRandomInt(int min, int max)
{
    return rnd.Next(min,max);
}
int myInt = GetRandomInt(5, 1000); //gives in random integer between 5 & 1000
 
Share this answer
 
May this code help you....
C#
public Random r = new Random();
List<string> l = new List<string>();

private void ButtonClick(object sender, EventArgs e)
{
    int temp = r.Next(1, 10);
    temp = RandomNumber(l, temp.ToString());
    listBox1.Items.Add(temp);
}
private int RandomNumber(List<string> listArray, string searchString)
{
    int temp;
    if (listArray.Count > 0)
    {
        re_View:
        bool isExists=l.Exists(delegate(string p){return p.Trim()==searchString;});
        if (isExists)
        {
             temp = r.Next(1, 10);
             searchString = temp.ToString();
             goto re_View;
        }
        else
        {
             l.Add(searchString);
             return Convert.ToInt32(searchString);
        }
    }
    l.Add(searchString);
    return Convert.ToInt32(searchString);
}
 
Share this answer
 

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