Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to generate a random integer value in PHP

5 Answers

0 votes
echo rand() . "<br />";
echo rand() . "<br />";
echo rand() . "<br />";

/*
run:

18542
2831
30620

*/

 



answered Aug 19, 2015 by avibootz
edited Feb 26, 2016 by avibootz
0 votes
for ($i = 0; $i < 20; $i++)
{
    $n = rand(2, 9);
    echo $n . "<br/>";
}
    
/*
run:

9
7
7
8
4
7
6
3
2
9
6
2
2
9
2
9
2
2
8
     
*/

 



answered Feb 26, 2016 by avibootz
0 votes
for ($i = 0; $i < 20; $i++)
{
    $n = rand(0, getrandmax());
    echo $n . "<br/>";
}
    
/*
run:

27969
742
39
8916
7293
26482
30659
6208
889
2238
19743
30828
24117
29386
7227
12120
10417
29078
16663
26884
     
*/

 



answered Feb 26, 2016 by avibootz
0 votes
for ($i = 0; $i < 20; $i++)
{
    echo mt_rand() . "<br/>";  // mt_rand() - Generate better random value
}
    
/*
run:

1593017367
1505266617
230869714
859889888
1330131806
95219024
780358501
666847360
1108311232
1190539088
1312662444
1537601971
1950352743
294675368
1798718792
980711402
629403232
1025377933
866511936
1107791016
     
*/

 



answered Feb 26, 2016 by avibootz
edited Feb 26, 2016 by avibootz
0 votes
for ($i = 0; $i < 20; $i++)
{
    echo mt_rand(0, mt_getrandmax()) . "<br/>";  // mt_rand() - Generate better random value
}
    
/*
run:

1931835025
1638704171
318262754
337344026
22731420
1029282866
1905830281
479038614
1582753727
633940063
801466444
58785175
936459425
1875809263
1426194725
889657451
1514011931
541808180
2077503657
970109280
     
*/

 



answered Feb 26, 2016 by avibootz
...