5

My question is probably very easy to answer. But I can't figure it out.

 <?php
    $id1 = rand(1,4);
    $id2 = rand(1,4);
    $id3 = rand(1,4);
    $id4 = rand(1,4);
  ?>

I have this. But this sometimes creates multiple 3's etc. But within my project this is something that can't happen. At the end the range will be between 1 and 100. But this is just to test how it could work. Can someone please help me?

5
  • If numbers aren't too many, try storing them them to an array and filter out repeat numbers. Commented Feb 14, 2017 at 14:09
  • 4
    Create an array from 1-4 and randomly sort the array. Commented Feb 14, 2017 at 14:11
  • If you generate 4 random numbers, it's even possible that they are all the same. With a short range as yours, that possibility is higher. What do you want at the end? The numbers 1-4 in random order, or with a limited number of repeats? Commented Feb 14, 2017 at 14:12
  • @Desaroll This is just a small reach. At the end I want 10 random numbers with a range of 1 to 100. Commented Feb 14, 2017 at 14:26
  • Related but with incorrect top answer: Generate array of random unique numbers in PHP Commented Mar 14 at 11:33

5 Answers 5

7

Create a range of values you can pick from. Shuffle it and take the first N results.

$range = range(0, 100); 
shuffle($range);
$n = 10;
$result = array_slice($range, 0 , $n);
Sign up to request clarification or add additional context in comments.

Comments

2

What is the purpose of this?

But, maybe you can simply do this:

$ids = range(1, 10);
shuffle($ids);

var_dump($ids);

/*
 * 1st Result:
 *
 * array (size=10)
 *   0 => int 10
 *   1 => int 2
 *   2 => int 5
 *   3 => int 7
 *   4 => int 9
 *   5 => int 1
 *   6 => int 3
 *   7 => int 6
 *   8 => int 4
 *   9 => int 8
 */

shuffle($ids);
var_dump($ids);

/*
 * 2nd Result:
 * 
 * array (size=10)
 *  0 => int 5
 *  1 => int 9
 *  2 => int 6
 *  3 => int 4
 *  4 => int 2
 *  5 => int 10
 *  6 => int 3
 *  7 => int 7
 *  8 => int 8
 *  9 => int 1
 * 
 */

3 Comments

Will this also help if you have a range between 1 and 100?
And my purpose is to take 10 questions out of a database with 100 questions in it.
Thank you so much.. This worked and I actually understood what I was doing ;) Really Appreciate it
0

Check this code:

$values=array();                 #preparint storage for numbers
$found=0;                        #number of found numbers

while($found!=4):                #script is looking for 4 numbers
    $v=rand(1,4);                #range of numbers (1-4)
    if(!in_array($v,$values)):   #if found number isn't in array
        $values[]=$v;            #add it
        $found++;                #and increment counter
    endif;
endwhile;

foreach($values as $value):
    echo '<p>'.$value.'</p>';    #echo found numbers
endforeach;

Script is prepared to find 4 random, unique numbers in range 1-4.

1 Comment

And do I need to delete my code that I have now for this to work? And what about doing this for multiple numbers? Or are you doing that here?
0
$temp = array() // global
$id1 = is_num_exists(rand(1,4));
$id2 = is_num_exists(rand(1,4));
$id3 = is_num_exists(rand(1,4));
$id4 = is_num_exists(rand(1,4));

function is_num_exists($id){
  while(in_array($id,$temp)){
    $id = rand(1,4);
  }
  $temp[] = $id;
  return $id
}

3 Comments

Can you please explain what you did? don't really understand why you make and return $id and $temp[].
Please describe your self, only code cannot answer the question.
this code is to generate unique random number between specified range, here is '1,4' it could be anything.
0

There are several approaches to achieve this:

  • Create an array of the given range (using the range() function) and shuffle it. To retrieve the values, you can walk it. This is useful if your range is not too large. That seems to be your case.

    $range = range(1, 10);
    shuffle($range);
    
  • If your range is much larger, you are better off storing your chosen values into a sorted array, checking each subsequent random number for being existent inside the array. That only goes for very large ranges, because not all possible values are loaded into memory, unlike the first example.

    $passedValues = array();
    for ($i = 0; $i < 10; $i++) {
        $v;
        do {
            $v = rand(1, 100);
        }
        while (in_array($passedValues));
        $passedValues[] = $v;
        sort($passedValues);
    }
    

The other answers provided some great code examples.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.