How to create a random number from 1 to a million?

I want to create a function that generates a random number from 1 to a million. and put it on the form

"You have won [random value] points. Congratulations."

and somehow save it in the variable "x", which I can call in the text box, so in the text box a value that:

<input class="textbox1" type="text" readonly value="[X]"> 
<p> [X] </p>
<a href="mywebsite" data-text="[X]"></a>

So I can use it in different places, how can I do it?

+4
source share
5 answers

Generate a random number from 1 to 1 million and save in $ randnumber; Then repeat the sentence. Finally, show this number in HTML by inserting the PHP code:

//...
$randnumber = rand(1, 1000000);

echo "You have won ".$randnumber." points. Congratulations.";
?>

<input class="textbox1" type="text" value="<?= $randnumber ?>" readonly> 
<p> <?= $randnumber ?> </p>
<a href="mywebsite" data-text="<?= $randnumber ?>">Text</a>

, .

+20

Javascript :

Math.floor((Math.random() * 1000000) + 1);

1 1 .

:

console.log(Math.floor((Math.random() * 1000000) + 1));
Hide result
+6

use php rand () function

rand(1, 1000000);
+5
source

Use this if you want to get better random numbers:

echo mt_rand(5, 15);

or visit here: http://php.net/manual/en/function.mt-rand.php

+4
source

// ... $ randnumber = rand (1, 1000000);

echo "You won." $ randnumber. "points. Congratulations."; ? >

"readonly>

0
source

All Articles