PHP随机数字1-20输出(PHP random number 1-20 output)
我试图输出20个图像,这些图像都在命名文件1-20.png中编号。 而不是输出他们,我想随机输出。
最初我有这个,但它显然是非常静态的,必须有一种方法可能使用foreach循环,但只是输出数字而不是数组。
另外,它不应该重复数字。
foreach (array_rand($numbers, 19) as $key)
echo '
?>
然后将数字1-20作为字符串存储在页面上的数组中。
I'm trying to output 20 images, which are all numbered in the named files 1-20.png. Instead of outputting them in order I want to randomise the output.
Originally I had this but it is obviously very static and there must be a way to maybe use the foreach loop but just output numbers rather than an array.
Also, it shouldn't repeat numbers.
foreach (array_rand($numbers, 19) as $key)
echo '
?>
Then had numbers 1-20 stored as string in an array on the page.
原文:https://stackoverflow.com/questions/22722284
更新时间:2020-01-11 21:18
最满意答案
像这样的东西:
$numbers = range(1, 20);
shuffle($numbers);
foreach ($numbers as $number) {
echo $number;
}
Maybe take a look at shuffle?
Something like this:
$numbers = range(1, 20);
shuffle($numbers);
foreach ($numbers as $number) {
echo $number;
}
2017-10-02
相关问答
$min=1;
$max=20;
echo rand($min,$max);
?>
$min=1;
$max=20;
echo rand($min,$max);
?>
试试这个,它应该工作: for(counter = 1; counter <= 20; counter++){
factorials *= counter;
System.out.printf("%4d%,30d\n", counter, factorials);
}
这样的for-loop更“干净”(java标准): for(x; x<=y; x++)然后在这些{}之间有其他逻辑/计算。 Try this, it should work: for(counter = 1; c
...
假设你有一个相对现代的bash,我建议如下: 1. for((...))循环而不是while : MAX_PROGRAM_NUM=9
for((i=1; i
echo "Checking program #$i"
PROGNAME="prog$i"
PIDFILE="${PROGNAME}.pid"
...
done
2.要检查文件的存在,请使用test -f 。 在pid文件
...
在这里,您有一个示例代码,您应该能够轻松地适应您的确切要求。 它包括两个按钮( newRandom和reset ):一个用于生成一个新的随机数(通过MessageBox显示),另一个用于删除存储到目前为止出现的所有数字的列表。 Dim alreadyPicked As List(Of Integer) = New List(Of Integer)
Private Sub newRandom_Click(sender As System.Object, e As System.EventArgs
...
也许看看shuffle ? 像这样的东西: <?php
$numbers = range(1, 20);
shuffle($numbers);
foreach ($numbers as $number) {
echo $number;
}
Maybe take a look at shuffle? Something like this: <?php
$numbers = range(1, 20);
shuffle($numbers);
foreach ($numbers as $
...
您想要创建一个普通的随机变量 。 以下函数使用Marsaglia极坐标方法创建一个变量: function rand_polar($m = 0.0, $s = 1.0){
do {
$x = (float)mt_rand()/(float)mt_getrandmax();
$y = (float)mt_rand()/(float)mt_getrandmax();
$q = pow((2 * $x - 1
...
试试这个.1.0~4.5的概率约为96%,4.5~10.0约为2%,10.0~20.0约为2%。 <?php
// 1.0~4.5 96%
// 4.5~10.0 2%
// 10.0~20.0 2%
function fun() {
$num = mt_rand(1, 100);
if ($num > 0 && $num <= 96) {
$return = mt_rand(10, 45) /
...
为什么你不能简单地通过euclid的算法找到gcd并将数字相乘并除以gcd来找到LCM? def decorator(d):
"""This is a decorator that will decorate all decorators. This will add update_wrapper
to all decorators.
(fun) -> fun
"""
def _d(f):
return update_wrapper(d(f)
...
除了你应该写成TrigB.Next(10, 21)以使其包含20 TrigB.Next(10, 21) ,你的代码很好。 对于Random.Next将Return Integer 如果得到小于1的值,则可能是调用Random.NextDouble而不是Random.Next 。 Except for the TrigB.Next(10, 20) which you should have written as TrigB.Next(10, 21) to make it inclusive of 2
...