Page 1 of 1
genrating random numbers
Posted: Mon Sep 22, 2008 1:53 am
by swetha
Code: Select all
$len=0;
$while ($len<8)
{
$value.=chr(mt_rand(33,47));
$len++;
}
echo $value;
[/color]
33 to 47,60 to 64,91 to 96,123 to 126.
the above values are the ascii values of punctuation.
In the following line(also shown in the above code),how do i include all the rest of the values
$value.=chr(mt_rand(33,47));
i.e i need to generate at one time,any one of these values randomly
$value=chr(mt_rand(33,47));
$value=chr(mt_rand(60,64));
$value=chr(mt_rand(91,96));
$value=chr(mt_rand(123,126));
pls help and thanks for any assistance.if any other clarifications required ,pls reply.
Re: genrating random numbers
Posted: Mon Sep 22, 2008 3:09 am
by Stryks
There is likely to be a better solution, but my first thought is to pack all those values into an array and just grab a random array element.
Something like ...
Code: Select all
<?php
$val_list = range(33,47);
$val_list = array_merge($val_list, range(60,64));
$val_list = array_merge($val_list, range(91,96));
$val_list = array_merge($val_list, range(123,126));
echo $val_list[array_rand($val_list)];
?>
If you arent confident that it is totally random, just finish off the list build section with ...
Anyhow ... Hope this is useful for you
Re: genrating random numbers
Posted: Mon Sep 22, 2008 3:38 am
by onion2k
Interesting approach there Stryks.. but I think it's a little more inefficient than it needs to be. I would do something like:
Code: Select all
switch (mt_rand(1,4)) {
case 1: $random=mt_rand(33,47); break;
case 2: $random=mt_rand(60,64); break;
case 3: $random=mt_rand(91,96); break;
case 4: $random=(mt_rand(123,126); break;
}
$value = chr($random);
I think that would be faster. Obviously we'd need to benchmark them to really know though, I can't be bothered doing that.

Re: genrating random numbers
Posted: Mon Sep 22, 2008 8:11 am
by Stryks
Hehehe ... nice solution ...
As always ... *wheres the 'not worthy' emoticon when you need it?*

Re: genrating random numbers
Posted: Tue Sep 23, 2008 4:31 am
by swetha
hey,i had already tried the option jedi mod has specified using the case statement.
Re: genrating random numbers
Posted: Tue Sep 23, 2008 4:48 am
by gethinw
Although the array solution is "more" random than the case solution, as the characters in the cases with less options (ie 60-64, compared to 33-47) are more likely to be picked, whereas each character is equally likely in the array solution. That's just me being pedantic though, and I suspect it's not really a problem...
Re: genrating random numbers
Posted: Tue Sep 23, 2008 6:14 am
by onion2k
gethinw wrote:Although the array solution is "more" random than the case solution, as the characters in the cases with less options (ie 60-64, compared to 33-47) are more likely to be picked, whereas each character is equally likely in the array solution. That's just me being pedantic though, and I suspect it's not really a problem...
That's a good point actually. I missed that.