1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Returning multiple unique random numbers

Discussion in 'PHP' started by jarvi, Feb 4, 2005.

  1. Allinpoker.cc

    Allinpoker.cc Peon

    Messages:
    252
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #21

    This was extremely helpful! Thank you!

    I looked for this for a long time around the search engines and PHP sites and couldn't find this info. Should have started at DP. Lesson learned.
     
    Allinpoker.cc, Nov 2, 2008 IP
  2. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #22
    wtf?
    1367 days old thread and only to post "Thanks" dude...
     
    ignas2526, Nov 2, 2008 IP
  3. tiggsy

    tiggsy Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #23
    The way I'm going to do it is:
    <?php
    $numbers = array(0,1,2,3,4,5,6,7,8,9);
    $selected = array_rand($numbers,4);
    ?>
    This will actually return the keys that point to the numbers, so you could put any items you want into the array (in the case of the array I've supplied, these are the same as the contents). I generally use this for rotating banners. I was quite surprised to find that rand() doesn't allow picking several numbers at once to avoid duplication.
     
    tiggsy, Aug 23, 2011 IP
  4. Didszzz

    Didszzz Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #24
    is there anyway i can get this code to not duplicate numbers when i click a button?
     
    Didszzz, Jan 22, 2012 IP
  5. Didszzz

    Didszzz Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #25
    how do i add code?
     
    Didszzz, Jan 22, 2012 IP
  6. tiggsy

    tiggsy Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #26
    You could generate numbers individually, store them to an array, check that the generated number is not already in the array, and if it is reject the number and generate a new one.
     
    tiggsy, Jan 23, 2012 IP
  7. Didszzz

    Didszzz Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #27
    thanks you for that! Could you help me a bit more on this im a noob at php im going through tiutorials at the moment but not confident enough to know how to do this!Thanks again
     
    Didszzz, Jan 25, 2012 IP
  8. tiggsy

    tiggsy Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #28
    OK. There are several ways to generate more than one unique random number in a range. The first is to store the range as an array and use array_rand to pick them all at the same time, as I explained in my earlier post:

    <?php
    $numbers = array(0=>0,1,2,3,4,5,6,7,8,9);
    $selected = array_rand($numbers,4);
    ?>

    Notes re above method:
    1. This is a cheat, really, because array_rand picks up keys, not the contents, and you will notice I've added 0=> inside the assignment brackets, because if your random number range doesn't start at 0 (and assuming your number set is whole numbers in a range incrementing by 1), you're going to have to assign the first one a key so that it works. This key should be the same as the first number in the range, so if your range is from 3 to 7, then the brackets would be (3=>3,4,5,6,7), and so on. I hope this is clear.
    2. The new variable $selected is an array containing the 4 numbers selected from the original array. To refer to each row in the array use $selected[0] through $selected[3]. If you want to put them in an echo statement use {} round them:
    <?php echo "The numbers are {$selected[0]}, {$selected[1]}, {$selected[2]} and {$selected[3]}"; ?>

    The second way, which has none of the previous one's problems, is to store all the numbers in an array, shuffle them, and then pop one value at a time off the array (which removes it from the array, so it can't be used again):

    <?php
    //set up the array sometime earlier and shuffle it
    $numbers = array(0,1,2,3,4,5,6,7,8,9);
    shuffle($numbers);
    //use the following command whenever you want to extract a single number from the array
    $selected = array_pop($numbers);
    ?>

    It's safest to use {} if you want to print it in an echo statement, as before: echo "The number is {$selected}"; ?>

    The third way is to store numbers as you generate them to an array, and to check the number isn't already present in the array whenever you generate a new one. However, having thought about this, I think you would be better using one of the solutions above, as they will be quicker.
     
    tiggsy, Jan 25, 2012 IP
  9. Dilir

    Dilir Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #29
    class UniqueRandomSet{
    static $questions=array();
    function __construct($questionsRequired,$questionsAvailable){
    for($n=0;$n<$questionsRequired;$n++){
    //print $n."<br/>";
    $x=rand(0,$questionsAvailable-1);
    if (in_array($x,self::$questions)) {
    $n--;
    }else{
    self::$questions[]=$x;
    }
    }
    //return $this->questions;
    }
    }
    //------------

    Test it
    -------
    $r=new UniqueRandomSet(5,33);
    for($n=0;$n<5;$n++){
    print UniqueRandomSet::$questions[$n]."<br/>";
    }
     
    Dilir, Jan 30, 2012 IP