开发者

How to Implement 'Lucky Draw' functionality in C#?

开发者 https://www.devze.com 2023-02-17 10:18 出处:网络
I am working a e-commerce website (in asp.net & c#) where which I want to implement a \'Lucky draw\' functionality.The control flow would be like this :- the users will be buying a particular prod

I am working a e-commerce website (in asp.net & c#) where which I want to implement a 'Lucky draw' functionality.The control flow would be like this :- the users will be buying a particular product on a day and I need to select a user from those who have purchased that开发者_Python百科 product as the luck draw winner for the day.

My initial thought on implementing the lucky draw was to use the Random functions provided by C#.And there are lots of thoughts came in to my mind like ..

  1. How effective it would be if I only use the random functions in c#?
  2. Would it be better if I implement any random number generation algorithms for this purpose ?
  3. Is there any algorithms available for random selections from a group?

I am open to your valuable comments and suggestions.

Thanks

Alex


  1. C# has no random functions. It gives you access to the .NET libraries.

  2. Don't use Math.Random. Do use one of the crypto RNGs that are provided by the .NET BCL. If you're doing this once or twice per day, the extra complexity of the crypto-strength RNG won't be a problem.


  1. The standard Random class should be enough, just make sure you use a unique seed everyday (say, a hash from every purchase data). Using time as seed (calling the Random constructor without parameter) could be bad if your app is always started in the exact same time everyday.
  2. The standard .NET library is good enough for statistical process. If you think you need stronger version, use the standard RNGCrypto library from .NET. Unless you got thousands of buyer everyday, I don't think you really need the crypto version. Don't implement an algorithm yourself. There's a reason people use standardized random library, because it's already tested against statistical requirement for randomness.
  3. Uhm... if you need to pick several lucky customers with equal probability, simply put them on a List, generate an integer between 0 and the List length. The integer you get is the first lucky customer, store it somewhere, remove it from the list, rinse and repeat for the next lucky customer. If you need to give higher probability according to their purchase count, store the purchase details instead of the customer inside the List. If you need custom weight according to the purchase value, use Fitness proportion, in which you total all purchase value for the day, then give each customer a cumulative value from their purchase. eg, if there are total $350 sale on that day, the first customer buying $75 got 75, the second customer buying $20 got 95 (75+20), the third customer buying $10 got 105 (95+10), etc. Then, pick a random integer between 0 and total sale, and the lucky winner is the customer with the lowest value that is still higher than the resulting integer. Remove the first winner from the list (both their total and position), rinse and repeat for the next customer.


In my opinion, its up to number of person you can select. If its just one, I would use Random() or as Ben Voigt suggested .

If its more complex lucky draw, you might need help from Combinations and Permutations algorithms.

0

精彩评论

暂无评论...
验证码 换一张
取 消