Professional Web Applications Themes

random subarray from an array - PHP Development

Hi, everybody First, the code: --------------------- $maxNo = 500 $numbers = range (1,$maxNo); srand ((double)microtime()*1000000); shuffle ($numbers); $subarray = array_slice ($numbers, 0, 24); ksort ($subarray); // this also don't work!!! while (list (, $number) = each ($subarray)) { echo "$number, "; } --------------------- I hoped this piece of code will generate random numbers from 1 to 500 but every time it generates the same sequence. Why? Also, when I change $maxNo to some other number, it changes the subarray, but then it stayes the same until I change the $maxNo again. If I change it back to 500, it's the ...

  1. #1

    Default random subarray from an array

    Hi, everybody

    First, the code:
    ---------------------
    $maxNo = 500
    $numbers = range (1,$maxNo);
    srand ((double)microtime()*1000000);
    shuffle ($numbers);
    $subarray = array_slice ($numbers, 0, 24);
    ksort ($subarray); // this also don't work!!!
    while (list (, $number) = each ($subarray)) {
    echo "$number, ";
    }
    ---------------------

    I hoped this piece of code will generate random numbers from 1 to 500
    but every time it generates the same sequence. Why?

    Also, when I change $maxNo to some other number, it changes the
    subarray, but then it stayes the same until I change the $maxNo again.
    If I change it back to 500, it's the first sequence all over again.
    This is the infamous sequence:
    30, 3, 482, 286, 226, 414, 158, 258, 98, 478, 222, 354, 94, 386, 130,
    350, 34, 481, 225, 290, 97, 418, 162, 353,

    This is definitely not how I understand word "random"!

    And one more thing: this example is how this thing works on my home
    computer. I have the same application on web server, and it doesn't
    work the same way! It changes subarray every time I refresh the page,
    but the numbers are inclined to upper limit, so if I put $maxNo = 500,
    10 out of 24 numbers will be between 480 and 500. Why they prefer
    upper limit?

    This is very confusing for me...

    Please, help

    Ante
    Ante Perkovic Guest

  2. #2

    Default Re: random subarray from an array

    Ante Perkovic wrote:
    > Hi, everybody
    >
    > First, the code:
    > ---------------------
    > $maxNo = 500
    > $numbers = range (1,$maxNo);
    > srand ((double)microtime()*1000000);
    try
    echo (double)microtime()*1000000;

    I get different values every time ... but you might not :)

    According to php.net, as of php 4.2.0 you needn't initialize the random
    number generator ... try leaving the srand() call out

    > shuffle ($numbers);
    > $subarray = array_slice ($numbers, 0, 24);
    > ksort ($subarray); // this also don't work!!!
    ksort() -- sort by key ... is this what you want?
    or is it a simple sort() ?

    > while (list (, $number) = each ($subarray)) {
    > echo "$number, ";
    > }
    maybe better:
    echo implode(', ', $subarray);

    > ---------------------
    >
    > but the numbers are inclined to upper limit, so if I put $maxNo = 500,
    > 10 out of 24 numbers will be between 480 and 500. Why they prefer
    > upper limit?
    This is strange!


    --
    I have a spam filter working.
    To mail me include "urkxvq" (with or without the quotes)
    in the subject line, or your mail will be ruthlessly discarded.
    Pedro Guest

Similar Threads

  1. Subarray
    By Christian Weber in forum PHP Development
    Replies: 6
    Last Post: August 3rd, 06:59 AM
  2. a random und array problem
    By Ratw? in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: February 22nd, 06:12 AM
  3. random array
    By Willem van Isselmuden in forum Macromedia Flash
    Replies: 0
    Last Post: September 21st, 09:49 PM
  4. Random Array values
    By mike in forum PHP Development
    Replies: 0
    Last Post: July 14th, 03:42 PM
  5. Problem with random array
    By noone in forum PHP Development
    Replies: 3
    Last Post: July 7th, 04:02 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139