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.

about Rand()

Discussion in 'PHP' started by stats, Sep 9, 2007.

  1. #1
    Is there a way to make the rand function some more selective ?

    here's what i want to do
    SEMrush
    imagine rand(0, 2000) .. it will return equally probable values from that range, right?
    so now i want it to return more probable values near 2000 then 0 .. in other words, i want to increase the probability near the max limit of the rand

    is there such a function to do that ?

    Thanks
     
    stats, Sep 9, 2007 IP
    SEMrush
  2. omgitsfletch

    omgitsfletch Well-Known Member

    Messages:
    1,222
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    145
    #2
    Hmm, that's a tough one. rand() and the more random mt_rand() both only accept a min and max argument. You could try something like this:

    
    $rand_adjust = rand(0,100);
    
    if($rand_adjust <= 50)
       $random = rand(0,2000);
    else
       $random = rand(1000,2000);
    
    PHP:
    So that way, you are controlling the probability in favor of higher values. You can obviously add to that and customize it to change the probability, but other than that, you can't affect the probability of a random number. ;)
     
    omgitsfletch, Sep 9, 2007 IP
    Bernard likes this.
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    
    
    $ranges = array(0, 1000, 1500);
    $rand = rand($ranges[array_rand($ranges)], 2000);
    
    echo $rand;
    
    
    PHP:
    This does the trick.
     
    nico_swd, Sep 9, 2007 IP
  4. omgitsfletch

    omgitsfletch Well-Known Member

    Messages:
    1,222
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    145
    #4
    That'll also work, and is admittedly a cleaner way of doing it, allowing you to store all the different adjustments in a single array.

    So for something like that:

    1/3 chance of 0-2000
    1/3 chance of 1000-2000
    1/3 chance of 1500-2000

    You'll end up with

    0-1000: (1/3 * 1/2 * 1/1000) = 1/6000 chance of each #
    1000-1500: ((1/3 * 1/4 * 1/500) + (1/3 * 1/2 * 1/500)) = 1/2000 chance of each #
    1500-2000: ((1/3 * 1/4 * 1/500) + (1/3 * 1/2 * 1/500) + (1/3 * 1 * 1/500)) = 35/30000 chance of each #

    Or

    0-1000 : 16.6% chance
    1000-1500: 25% chance
    1500-2000: 58.3% chance

    Now you can see, it's fairly easy to customize the rand to suit your needs.
     
    omgitsfletch, Sep 9, 2007 IP
  5. stats

    stats Well-Known Member

    Messages:
    579
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Thanks ! :)
     
    stats, Sep 9, 2007 IP