PHP20个随机数从大到小排列,生成两个随机数,第一个数小于第二个php(Generate two random numbers first number smaller than second ph...

生成两个随机数,第一个数小于第二个php(Generate two random numbers first number smaller than second php)

我需要使用PHP生成两个随机数。 这很简单,但条件是第一个数字应小于第二个数字值。

所以我要说我需要生成10到1000之间的下限和上限。所以我需要在这个范围内有两个随机数。 第一个数字作为下限(20可以是),上限高于值,因此(100可以是)。

谢谢艾哈迈尔。

I need to generate two Random numbers using PHP. This is simple but the condition is that the first number should be smaller than second number in value.

So let's say I need to generate lower and upper bounds between 10 and 1000. So I will need two random numbers within this range. First number as lower bound (20 may be) and upper bound higher than that in value so (100 may be).

Thanks Ahmar.

原文:https://stackoverflow.com/questions/14472770

更新时间:2020-04-27 20:04

最满意答案

$num1 = rand(20, 999);

$num2 = rand($num1+1, 1000);

$num1 = rand(20, 999);

$num2 = rand($num1+1, 1000);

2013-01-23

相关问答

如果你想要在1到6之间,你可以计算: Math.floor(Math.random() * 6) + 1

哪里: 1是起始号码 6是可能结果的数量(1 +起始(6) - 结束(1) ) Important The following code works only if the minimum value is 1. It does not work for minimum values other than 1. If you wanted to get a random integer

...

1.这是一种方式: for($i = 0; $i < 116; $i++){

$common[] = array(mt_rand(1, 22), mt_rand(34, 76))[mt_rand(0, 1)];

}

创建一个包含两个随机数的数组,每个范围一个 随机选择数组索引0或1 好吧,我很无聊,这是另一个: $range = array_merge(range(1, 22), range(34, 76));

for($i = 0; $i < 116; $i++){

$com

...

生成0到2之间的随机数,然后从生成的数字中减去1。 Generate random number between 0 and 2, then subtract 1 from generated number.

基本实现需要错误检查+不是100%随机 <?php

//first initial number

// range must be 3-15 to get 3 random additions

$i = rand(3,15);

echo $i;

// generate what 3 number adds to $i

$one = rand(1,$i-2);

$i-=$one;

$two = rand(1,$i-1);

$i -= $two;

$three = rand(1,$i);

$i -=

...

如何订购随机数列表...... public static void method2() throws Exception {

Random rng = new SecureRandom();

Set numbers = new HashSet<>();

while (numbers.size() < 3) {

// number only added if not already present in Set, set values ar

...

你可以检查它的减号。 检查number2是否大于number1然后交换它们。 像这样的东西: if(plusMinus.textContent == '-' && number2.textContent > number1.textContent) {

var a = number2.textContent;

number2.textContent = number1.textContent;

number1.textContent = a;

}

当您调用answer()

...

你经常得1-6,因为你从哈希中获取两个十六进制数字。 这是一个字节,因此它可以存储0到255值。 然后你使用modulo 50 。 结果你得到范围200-249和...... 250-255 。 最后一个是造成结果中1-6的额外流行的原因。 解决方案:只需使用mt_rand(1,50); [编辑] 如果您确实需要将0-255范围内的数字转换为1-50范围,则解决方案将是缩放和舍入。 $number = round(($byteValue)/(255/49))+1;

You get 1-6 mo

...

这是一个可以使用的方法: <?php

$num= mt_rand();

$con = mysql_connect("localhost","uname","password");

mysql_select_db("dbname",$con);

$sel_query = "SELECT * FROM my_table WHERE rand_num =%d"; // query to select value

$ins_query = "INSERT INTO my_table(rand_nu

...

试试这个.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) /

...

$num1 = rand(20, 999);

$num2 = rand($num1+1, 1000);

$num1 = rand(20, 999);

$num2 = rand($num1+1, 1000);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值