[英]How to generate a random number between 1 and a million?
I want to make a function the generates a random number from 1 to million .. and put it in a form of 我想创建一个函数,生成一个从1到百万的随机数..并把它放在一个形式
"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: 并以某种方式将其保存到变量“x”,我可以在文本框中调用,所以在文本框中的值如下:
<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 ? 所以我可以在不同的地方使用它,我怎么能这样做?
Generate random number between 1 and 1 million and save in $randnumber; 生成1到100万之间的随机数并保存在$ randnumber中; Then echo the sentence.
然后回应句子。 Finally, show that number in HTML by inserting PHP code:
最后,通过插入PHP代码在HTML中显示该数字:
//...
$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: 在Javascript中,您可以使用:
Math.floor((Math.random() * 1000000) + 1);
This will generate a number between 1 and 1 million. 这将生成1到100万之间的数字。
A snippet to test: 要测试的代码段:
console.log(Math.floor((Math.random() * 1000000) + 1));
使用php函数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 或访问: http : //php.net/manual/en/function.mt-rand.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.