James Pale
James Pale

Reputation: 185

echo out random numbers 0.5 to 1?

hi i am trying to use php to echo out a random set of numbers from 0.5 all the way to 1; so 05, 0.6, 0.7, 0.8 > 1

i am currently echoing out a list of users and so need these numbers to be echoed out with each user randomly.

is there an easy way to do this?

<?php
    $local_set = get_local_users();
    $local_count = mysql_num_rows($local_set);
    while ($local = mysql_fetch_array($local_set)) {

        echo "<div class=\"sugarushcase\">
        <a href=\"profile.php?id={$local['id']}\"><img width=80px height= 80px src=\"data/photos/{$local['id']}/_default.jpg\" class=\"boxgrid\"/></a><div class=\"local_text\">about (RANDOM NUBER) mile</div>
        </div>";

    }

Upvotes: 0

Views: 438

Answers (3)

Mattios550
Mattios550

Reputation: 372

Yep, here is your edited code.

<?php
$local_set = get_local_users();
$local_count = mysql_num_rows($local_set);
$random_number = mt_rand (0.5*10, 1.0*10) / 10; //Edit min/max values here
while ($local = mysql_fetch_array($local_set)) {

    echo "<div class=\"sugarushcase\">
    <a href=\"profile.php?id={$local['id']}\"><img width=80px height= 80px src=\"data/photos/{$local['id']}/_default.jpg\" class=\"boxgrid\"/></a><div class=\"local_text\">about {$random_number} mile</div>
    </div>";

}

It was written without being tested so comment if it doesn't work & I'll look into it :)

Upvotes: 0

Mathew Thompson
Mathew Thompson

Reputation: 56439

You can use rand to achieve this, passing in the min and max:

rand (5, 10) / 10

Your code would then be:

echo "<div class=\"sugarushcase\">
    <a href=\"profile.php?id={$local['id']}\"><img width=80px height= 80px" + 
    "src=\"data/photos/{$local['id']}/_default.jpg\" class=\"boxgrid\"/></a><div class=\"local_text\">about" + 
    (rand (5, 10) / 10) + "mile</div></div>";

Upvotes: 1

rid
rid

Reputation: 63542

Use mt_rand():

$rand = mt_rand(5, 10) / 10;

You could calculate this random number for every iteration, right before echo, then use $rand instead of (RANDOM NUMBER) in your example.

Upvotes: 3

Related Questions