php随机输出20个数,PHP随机数字1-20输出(PHP random number 1-20 output)

该问题讨论了如何在PHP中使用`shuffle`函数随机输出1到20之间的不重复数字,这些数字对应于文件名1-20.png的图像。原始代码使用`array_rand`产生随机索引,但需求是连续输出不重复的数字。解决方案是创建一个包含1到20的数组,然后使用`shuffle`打乱顺序,最后遍历数组输出数字。
摘要由CSDN通过智能技术生成

PHP随机数字1-20输出(PHP random number 1-20 output)

我试图输出20个图像,这些图像都在命名文件1-20.png中编号。 而不是输出他们,我想随机输出。

最初我有这个,但它显然是非常静态的,必须有一种方法可能使用foreach循环,但只是输出数字而不是数组。

另外,它不应该重复数字。

foreach (array_rand($numbers, 19) as $key)

echo '

'%20.%20%24numbers%5B%24key%5D%20.%20'.png
';

?>

然后将数字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 '

'%20.%20%24numbers%5B%24key%5D%20.%20'.png
';

?>

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

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值