2

I want to create a random number using rand() Where the number != to ( 516 for instance )

for instance if I have number "5" in the database and I want to insert a random number != 5.

PHPGEEK
  • 55
  • 5
  • Wouldn't be super efficient, but you could just generate a number in an infinite loop, and only break out of it if the generated number != your excluded number. I can't think of a way to straight generate a random number, excluding a certain number. – Carcigenicate Apr 17 '15 at 02:39
  • 3
    This sounds suspiciously like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Please explain why the number needs to be 'random'. – p.s.w.g Apr 17 '15 at 02:41
  • I would think a do/while statement would work out well here. Need more example code. – Twisty Apr 17 '15 at 02:47
  • @Carcigenicate its not gonna work because I have a list of numbers in the database and I want to generate a number != to all of them. – PHPGEEK Apr 17 '15 at 03:14
  • does it have to be random? why not just get the max value from the database and add 1 – pala_ Apr 17 '15 at 03:14
  • @p.s.w.g it has to be random because I am creating a secure login system based on cookies. – PHPGEEK Apr 17 '15 at 03:16
  • @Twisty loop statements wont work since I am looking for one number != to all numbers in the database. – PHPGEEK Apr 17 '15 at 03:16
  • 3
    I would advice using [uuid](http://stackoverflow.com/a/15875555/1338292) instead. – Ja͢ck Apr 17 '15 at 03:17
  • there are better ways to generate unique values than to use rand() over and over. – pala_ Apr 17 '15 at 03:17
  • @pala_ it has to be random because I am creating a secure login system based on cookies, so the number has to be unpredictable. – PHPGEEK Apr 17 '15 at 03:17
  • If the intent is to generate secure (i.e. cryptographically secure) random numbers, see [this question](http://stackoverflow.com/questions/1182584/secure-random-number-generation-in-php). – p.s.w.g Apr 17 '15 at 03:19
  • If you want to pull a random number and make sure it isn't in your DB already this might be useful. http://stackoverflow.com/questions/11680025/how-to-generate-random-number-without-repeat-in-database-using-php – chris85 Apr 17 '15 at 03:24

4 Answers4

2

One solution...

$disallow = 5;
$random_value = $disallow;
while($random_value == $disallow) {
    $random_value = rand();
} 
echo $random_value;

Documentation on rand, http://php.net/manual/en/function.rand.php.

chris85
  • 23,846
  • 7
  • 34
  • 51
1

this one will exclude all values given in an array.

$excluded_nums = array( .... );
$val = null;
do {
  $val = rand();
} while (in_array($val, $excluded_nums));

but you may be interested in exploring another method, perhaps openssl_random_pseudo_bytes

pala_
  • 8,901
  • 1
  • 15
  • 32
1

I am creating a secure login system based on cookies, so the number has to be unpredictable.

Instead of generating numbers, it's recommended to generate an opaque identifier such as UUID v4 that's meant to be unpredictable (provided a proper entropy source is used); an example identifier:

de305d54-75b4-431b-adb2-eb6b9e546014

I've created a function to generate this identifier in an earlier answer.

I have a list of numbers in the database and I want to generate a number != to all of them

The approach I would recommend is to add a UNIQUE constraint on the identifier and write a loop that generates a new identifier and attempts to insert it; it ends when the insertion doesn't cause a constraint error; to put the odds in perspective, you're more likely to get hit by a meteorite than hitting a duplicate value.

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
-1

Use arrays and rand:

$ran_begin = array(1,2,3,4,6,7,8,9);//not contain 5 and 0
$ran = array(0,1,2,3,4,5,6,7,8,9);

$random_value = '';
$k = array_rand($ran_begin);
$v = $array[$k];
$random_value = $random_value.$v;

$k = array_rand($ran);
$v = $array[$k];
$random_value = $random_value.$v;


$k = array_rand($ran);
$v = $array[$k];
$random_value = $random_value.$v;

..............................

Asif Iqbal
  • 1,132
  • 1
  • 10
  • 23