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.

how to code php script that can generate random number array .

Discussion in 'PHP' started by justinlorder, Jan 19, 2009.

  1. #1
    how to code php script that can generate random number array .

    For example, there are 25 numbers from 1 to 25 .
    Each time, the script can randomly generate five number out of the 25 numbers .
    The five numbers never repeat, I call them one number array .
    If the sum of the five number is between 35 and 60, then print the number array.
    If not, ignore it .
     
    justinlorder, Jan 19, 2009 IP
  2. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #2
    
    <?php
    $numbers = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25);
    shuffle($numbers);
    
    $number1 = $numbers[0];
    $number2 = $numbers[1];
    $number3 = $numbers[2];
    $number4 = $numbers[3];
    $number5 = $numbers[4];
    $total  = $number1 + $number2 + $number3 + $number4 + $number5;
    
    if($total >= 35 and $total <= 60){
    	echo $total;
    }
    ?>
    
    PHP:
    Are you looking for something like that? You can simplify it a little more, but I wrote it all out so you can see what the script was doing. If you want the simplifier version you can use this

    
    <?php
    $numbers = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25);
    shuffle($numbers);
    
    $total  = $numbers[0]; + $numbers[1]; + $numbers[2]; + $numbers[3]; + $numbers[4];
    
    if($total >= 35 and $total <= 60){
    	echo $total;
    }
    ?>
    
    PHP:
    When you use the shuffle function it will randomly shuffle everything in the array. Then from there you simply call the first 5 numbers from the array (or how ever many numbers you want to call).
     
    bigrollerdave, Jan 19, 2009 IP
    LogicFlux and justinlorder like this.
  3. justinlorder

    justinlorder Peon

    Messages:
    4,160
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #3
    bigrollerdave
    I appreciate your feedback .
    how to echo the number array rather than echo the $total . lol
    Also I know little about php .
    Does the code meet the requirement "the five number never repeat" ?
     
    justinlorder, Jan 19, 2009 IP
  4. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #4
    You want to echo the 5 total numbers?

    Well it's impossible to never have a number repeat when using a set amount of numbers. If I gave you 25 dice and you rolled them 1 million times the chance of you rolling the same exact thing twice will be really slim. Although it is possible to have the same numbers twice. Using the shuffle though will shuffle up the array every time someone visits the page though so it will almost never repeat for the same user, if that makes sense.
     
    bigrollerdave, Jan 19, 2009 IP
  5. justinlorder

    justinlorder Peon

    Messages:
    4,160
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes, I want to echo the five numbers .
     
    justinlorder, Jan 19, 2009 IP
  6. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #6
    
    <?php
    $numbers = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25);
    shuffle($numbers);
    
    $total  = $numbers[0] + $numbers[1] + $numbers[2] + $numbers[3] + $numbers[4];
    
    if($total >= 35 and $total <= 60){
    	echo $numbers[0] .' '. $numbers[1] .' '. $numbers[2] .' '. $numbers[3] .' '. $numbers[4];
    	
    }
    ?>
    
    PHP:
    There you go that will not display all 5 numbers in the array if the total is above or equal to 35 and less then or equal to 60
     
    bigrollerdave, Jan 19, 2009 IP
  7. justinlorder

    justinlorder Peon

    Messages:
    4,160
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks , I get it .
    bigrollerdave is really a helpful dp member .
     
    justinlorder, Jan 19, 2009 IP
  8. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #8
    No problem, glad I could help.
     
    bigrollerdave, Jan 19, 2009 IP
  9. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #9
    @bigrollerdave:

    The range() function could save you some time if you find you need to generate a list of numbers again.
     
    Danltn, Jan 19, 2009 IP
  10. paitken

    paitken Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Here is some cleaner code, it's untested, but should work.
    
    $numbers = range(1, 25);
    shuffle($numbers);
    $new_nums = array_slice($numbers, 0, 5);
    $total = array_sum($new_nums);
    if($total >= 35 && $total <= 60) {
    	echo implode('-', $new_nums);
    }
    
    PHP:
     
    paitken, Jan 19, 2009 IP
  11. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #11
    Yeah, I like to display code simply for those who don't have a strong grasp of php. I like to make the code understandable so they will not only be able to use it, but understand it. The range function makes it a little simpler but it's harder to get a grasp of.
     
    bigrollerdave, Jan 20, 2009 IP
  12. paitken

    paitken Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Not really an excuse. If you put something they don't know, they should goto http://php.net/range <- They have examples and show basics.
     
    paitken, Jan 25, 2009 IP