没有任何重复的随机数 [英] without any repeat random number

查看:108
本文介绍了没有任何重复的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我想在读取数据库的(min,max)个值之间创建一个随机数,然后选择一个保存在数据库中的数字.
我的程序是抽奖,没有重复号码.有人可以帮助我吗?

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??

推荐答案

像上周这个人想要的吗?

如何检查随机值并避免使用相同的随机值? [
Something like what this guy wanted last week?

How to check a random value and avoid for the same random value?[^]


如果您需要一种避免重复随机数的算法,则可能需要看看我的提示:随机抽取5张卡从甲板上" [
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).
:-)


按照步骤操作.

步骤1.在cs文件中创建方法VerifyInDB(string/int randomvariable),如下所示
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;

}



步骤2.在相同的* .cs文件中创建另一个方法,如下所示



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;

}


步骤3:在代码中的任何位置调用CreateRandomNumber(),这将返回数据库中不存在的随机值.

步骤4:int/String RandumValue = CreateRandomNumber();您将获得在数据库中不会重复的随机值.


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.


这篇关于没有任何重复的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆