PHP loop increment randomly?

We all know the basic

$i = 1;

while ($i<100){
    echo $i;
    $i++
}

Question: How do I increment $i by a random number between 1 and 5 each time it loops?

Answers


Exactly like you described it in words: By increment it with a random number between 1 and 5.

while ($i < 1000) {
  echo $i;
  $i += rand(1,5);
}

rand()


In one line:

for ($i = 1; $i < 1000; $i += rand(1, 5)) echo $i;

mt_rand is faster and uses uses the Mersenne Twister algorythm (1997)

while ($i < 1000) {
  echo $i;
  $i += mt_rand(1,5);
}

Need Your Help

Convert PDF File to HTML in C#

c# .net pdf webbrowser-control

I had a problem highlighting text in a pdf file embedded in webbrowser control and highlighting text using PDFLibNet.pdfwrapper so i'm shifting to another process where i'll just convert the pdf to...

How to keep an image in an Android activity during an orientation change?

android image bitmap imageview orientation

I am very new to Android programming and have been experimenting with building an app. In my new app I have the user press a button which allows them to choose an app on their device such as 'Gall...