How to generate unique random value for each user

2019-03-29 06:56发布

I am developing a event organization website. Here when the user registers for an event he will be given a unique random number(10 digit), which we use to generate a barcode and mail it to him. Now,

  1. I want to make the number unique for each registered event.
  2. And also random

One solution is to grab all the random numbers in an array and generate a random number using Php rand(1000000000, 9999999999) and loop through and check all the values. Grab the first value that doesn't equal to any of the values in the array and add it to the database.

But I am thinking that there might be a better solution to this. Any suggestion?

7条回答
一纸荒年 Trace。
2楼-- · 2019-03-29 07:22

This is good:

do {
   $refrence_id = mt_rand( 1000000000, 9999999999 );
} while ( DB::table( 'transations' )->where( 'RefrenceID', $refrence_id )->exists() );
查看更多
登录 后发表回答