Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Now i want to select the 1 random Number from database records 1-10000 in the given range.And also check the random number is already exist are not if exist means select new number.
Posted
Updated 19-Jun-15 19:31pm
v4
Comments
F-ES Sitecore 19-Jun-15 5:46am    
What have you tried so far? Is there a specific part of this task you are stuck at? The way you have worded it, it seems to be a request for someone else to do your work for you.
venkatesh@India 19-Jun-15 5:59am    
Generated random number adding in arraylist check already exist not.Working fine .But i want alternate way to do this.

I would insert then numbers 1..1000 in a container (e.g. a List<int>) then remove all the ones already recorded in the database and eventually randomly choose among the remining ones.
 
Share this answer
 
Comments
Maciej Los 19-Jun-15 5:53am    
Very good advice!
CPallini 20-Jun-15 11:58am    
Thank you, Maciej.
venkatesh@India 19-Jun-15 6:02am    
If i remove the records.But more number no users will do same action means getting error.
Philippe Mori 22-Jun-15 12:43pm    
Not clear!
---Create Below function to generate Random Number

CREATE FUNCTION dbo.RandomNumber()
RETURNS INT
AS
BEGIN
DECLARE @Result INT

SET @Result = CRYPT_GEN_RANDOM(2)

RETURN CASE
WHEN @Result < 60000
OR @@NESTLEVEL = 32 THEN @Result % 10000
ELSE dbo.RandomNumber()
END
END

---Use function to insert generated Random Number

DECLARE @Number INT
GenerateNumber:
select @Number=dbo.RandomNumber()
If Exists(select * from [Table] where number=@Number)
GOTO GenerateNumber;
Else
Insert @Number
 
Share this answer
 
v2
Comments
venkatesh@India 19-Jun-15 7:59am    
Now i am using mysql .
jaideepsinh 19-Jun-15 9:11am    
Use this for MYSql
select FLOOR(RAND( )*10000)
venkatesh@India 20-Jun-15 1:34am    
not working.
C#
do
  {
      number = random.Next();
  } while (randomNumbers.Contains(number));



try this
 
Share this answer
 
Comments
Philippe Mori 22-Jun-15 12:42pm    
This will eventually cause an infinite loop or major slowdown when the list is becoming empty.

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