简体   繁体   English

如何生成1到100万之间的随机数?

[英]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.

相关问题 Javascript/html:如何在数字 A 和数字 B 之间生成随机数? - Javascript/html: How to generate random number between number A and number B? 如何使用sha256哈希生成0到1之间的随机数 - How to generate random number between 0 and 1 using a sha256 hash 我如何生成1到10之间的随机数,但随机数不能为3 - How can I generate a random number between 1 - 10 except that the random number can't be 3 在 JavaScript 中的两个数字之间生成随机数 - Generate random number between two numbers in JavaScript 如何生成随机素数? - How to generate random prime number? 如何生成随机发票号 - How to generate a random invoice number 如何在两个数字之间生成任意随机数的Javascript设备如何以数学方式工作? - How does the Javascript device that generate any random number between two number inclusive work mathematically? JavaScript - 生成 1 到 5 之间的随机数,但不要连续两次生成相同的数字 - JavaScript - Generate a random number between 1 and 5, but never the same number twice consecutively 在x和y之间生成随机数但在a和b之间不生成随机数的最佳方法 - Best way to generate random number between x and y but not between a and b 如何在两个值之间生成一个随机数,优先选择接近极限的数字 - How to generate a random number between two values with preference for numbers closer to the limits
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM