admin管理员组

文章数量:1204016

I want to make a function the generates a random number from 1 to million .. and put it in a form of

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

and somehow save it into variable "x" that i can call in a text box so in the text box value which like:

<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 could I do it ?

I want to make a function the generates a random number from 1 to million .. and put it in a form of

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

and somehow save it into variable "x" that i can call in a text box so in the text box value which like:

<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 could I do it ?

Share Improve this question edited Apr 2, 2018 at 14:06 Kristianmitk 4,7785 gold badges29 silver badges49 bronze badges asked May 27, 2016 at 6:28 Abdulrahman AzmyAbdulrahman Azmy 692 silver badges9 bronze badges 3
  • 1 use array_rand or rand of PHP functions... – Murad Hasan Commented May 27, 2016 at 6:30
  • 1 You want to do this using php or js? – Mohammad Commented May 27, 2016 at 7:14
  • 1 If there was an error (since you asked again), you could just notify me in here, there was no need to create new question about the same thing. You may also accept the answer as it really has no syntax errors and does what you asked for. :) – Gynteniuxas Commented May 27, 2016 at 10:44
Add a comment  | 

4 Answers 4

Reset to default 18

Generate random number between 1 and 1 million and save in $randnumber; Then echo the sentence. Finally, show that number in HTML by inserting 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>

I think there are no syntax errors.

In Javascript you can use:

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

This will generate a number between 1 and 1 million.

A snippet to test:

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

use php function rand()

rand(1, 1000000);

Use this if want better random numbers:

echo mt_rand(5, 15);

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

本文标签: javascriptHow to generate a random number between 1 and a millionStack Overflow