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 |4 Answers
Reset to default 18Generate 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
版权声明:本文标题:javascript - How to generate a random number between 1 and a million? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738672398a2106071.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
array_rand
orrand
of PHP functions... – Murad Hasan Commented May 27, 2016 at 6:30php
orjs
? – Mohammad Commented May 27, 2016 at 7:14