简体   繁体   English

mt_rand()给了我相同的数字

[英]mt_rand() gives me always the same number

I have a problem with this function always my mt_rand() give me the same number: 我有这个函数的问题总是我的mt_rand()给我相同的数字:

$hex = 'f12a218a7dd76fb5924f5deb1ef75a889eba4724e55e6568cf30be634706bd4c'; // i edit this string for each request
$hex = hexdec($hex);    
mt_srand($hex);
$hex = sprintf("%04d", mt_rand('0','9999'));

$hex is always changed, but the result is always the same 4488 . $hex总是被改变,但结果总是相同4488

Edit 编辑

$hex = str_split($hex);
$hex = implode("", array_slice($hex, 0, 7));
mt_srand($hex);
$number = sprintf("%04d", mt_rand('0','9999'));

http://php.net/manual/en/function.mt-srand.php http://php.net/manual/en/function.mt-srand.php

Your problem is, that you always end up with a float value in your variable $hex . 您的问题是,您总是在变量$hex使用浮点值。 And the function mt_srand() as you can also see in the manual : 你可以在手册中看到函数mt_srand()

void mt_srand ([ int $seed ] ) void mt_srand([ int $ seed])

Expects an integer. 期待一个整数。 So what it does is, it simply tries to convert your float value to an integer. 它的作用是,它只是尝试将您的浮点值转换为整数。 But since this fails it will always return 0. 但由于失败,它将始终返回0。

So at the end you always end up with the seed 0 and then also with the same "random" number. 所以最后你总是得到种子0然后也有相同的“随机”数字。

You can see this if you do: 如果你这样做,你可以看到这个:

var_dump($hex);

output: 输出:

float(1.0908183557664E+77)

And if you then want to see in which integer it will end up if it gets converted you can use this: 如果你想看看哪个整数最终会转换,你可以使用:

var_dump((int)$hex);

And you will see it will always be 0. 你会发现它总是0。


Also if you are interested in, why your number ends up as float, it's simply because of the integer overflow, since your number is way too big and accoding to the manual : 此外,如果你感兴趣,为什么你的数字最终为浮动,这只是因为整数溢出,因为你的数字太大而且符合手册

If PHP encounters a number beyond the bounds of the integer type , it will be interpreted as a float instead . 如果PHP遇到超出整数类型边界的数字,它将被解释为浮点数 Also, an operation which results in a number beyond the bounds of the integer type will return a float instead. 此外,导致超出整数类型边界的数字的操作将返回浮点数。

And if you do: 如果你这样做:

echo PHP_INT_MAX;

You will get the max value of int, which will be: 你将获得int的最大值,它将是:

28192147483647      //32 bit
9223372036854775807 //64 bit

EDIT: 编辑:

So now how to fix this problem and still make sure to get a random number? 那么现在如何解决这个问题仍然确保得到一个随机数?

Well the first thought could be just to check if the value is bigger than PHP_INT_MAX and if yes set it to some other number. 那么第一个想法可能只是检查值是否大于PHP_INT_MAX,如果是,则将其设置为其他数字。 But I assume and it seems like you will always have such a large hex number. 但我认为,似乎你总会有这么大的十六进制数。

So I would recommend you something like this: 所以我建议你这样的东西:

$arr = str_split($hex);
shuffle($arr);
$hex = implode("", array_slice($arr, 0, 7));

Here I just split your number into an array with str_split() , to then shuffle() the array and after this I implode() the first 7 array elements which I get with array_slice() . 在这里,我只是拆你的电话号码与一个数组str_split()然后到shuffle()的阵列,这之后我implode()第7数组元素,我用得到array_slice()

After you have done this you can use it with hexdec() and then use it in mt_srand() . 完成此操作后,您可以将它与hexdec()使用,然后在mt_srand()使用它。

Also why do I only get the first 7 elements? 另外,为什么我只获得前7个元素? Simply because then I can make sure I don't hit the PHP_INT_MAX value. 因为那时我可以确保我没有达到PHP_INT_MAX值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM