0

I have requirement where i have to generate more than 4 lacs (almost half a million) random numbers. These are my different vouchers to distribute to my e-commerce customers. I am using Entity framework and am calling sql stored procedure using "ExecuteStoreQuery" method. And in SQL server i am using Left(newid(),7) function to generate these numbers. So basically this function returns left 7 digits of 16 digits generated random numbers in sql. This sp takes almost 1.5 to 2 mintues to generate 450K numbers which is fine and OK. There is no issue for less than 50k numbers, but for more than that i am getting duplicate numbers. I can not use whole 16 digit number as it is quite long for Voucher to end user to enter.

Kindly help. I m using ASP.net MVC 4.0 with EF 6.0 with SQL Server 2008 R2.

Thanks in advance.

user3851995
  • 37
  • 1
  • 1
  • 5
  • Left(newid(),7) returns an alphanumeric string, so letters are ok? or are you converting to decimal? – Alex K. Aug 05 '14 at 12:54
  • Do it in code with a lookup to suppress dups; http://stackoverflow.com/questions/14473321/generating-random-unique-values-c-sharp – Alex K. Aug 05 '14 at 12:55
  • Hi Alex,Yes letters are ok. But i think lookup will take too much time and impact the performance of the application. – user3851995 Aug 05 '14 at 13:10
  • Are you not just generating them *once* then allocating as needed? – Alex K. Aug 05 '14 at 13:11
  • No. Currently i am using sql to generate it and insert using stored procedure. Should i use code instead in that case i will need to create collection of 450K and use sql to insert? Please correct me if i am wrong. Thanks. – user3851995 Aug 05 '14 at 13:14
  • Create 450k random unique values - once - Put them in a table with an "assigned" column, then when you need one just query for it updating the assigned value – Alex K. Aug 05 '14 at 13:16
  • @AlexK. I think the problem is creating the 450k unique values in the first place – DavidG Aug 05 '14 at 13:17
  • What if you had 450k sequential "values" in a table, but you ordered them randomly? That might make it easier to adhere to the uniqueness requirement. – Dave Mason Aug 05 '14 at 13:29
  • Yes i think David is right, first thing is to create random 450k values without impacting performance of the application. We have to keep in mind that this will be used as voucher. So it should not be kind of sequential like from 1 to 450k or kind of more predictable. May be to clarify need around 450k random with unique 7 or 9 characters code. If this helps. – user3851995 Aug 05 '14 at 14:05

0 Answers0