0

Im trying to create an array with 100 random values from 1 to 1000, and multiply each by 4

so far I have:

$numbers = array(rand(1, 1000),rand(1, 1000),rand(1, 1000),
    rand(1, 1000),rand(1, 1000),rand(1, 1000)

for($x=0; $x<100; $x++)
    echo $numbers[$x]*4 . "<br/>";

How do I get rand(1,1000) to repeat 100 times without copy pasting? thanks!

4 Answers 4

2
<?php
$numbers = array();

for ($i = 0; $i < 100; $i++)
    $numbers[] = rand(1, 1000);

foreach ($numbers as $number)
    echo ($number * 4) . "<br />";
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried this or I misunderstood your question:

$randArray=array();
for($x=0;$x<100,$x++)
{
    $randArray[]=rand(1,1000)*4;

    //echo $randArray[$x];
}

Comments

0
$numbers = array();
for ($x = 0; $x < 100; $x++) {
    $numbers[] = rand(1,1000) * 4 . "<br>";
}

You could of course loop again if you need to have the values without them being multiplied by 4 for some reason.

Another possible solution without the inelegant loop:

array_map(function () { return rand(1,1000); }, range(1,1000));

Comments

0
<?php
$numbers = array();
for($i = 0; $i < 100; $i ++)
{
    $numbers[] = rand(1, 1000);
    echo $numbers[$i] * 4 . "<br/>\n";
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.