genrating random numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

genrating random numbers

Post 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.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: genrating random numbers

Post 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 ...

Code: Select all

  shuffle($val_list);


Anyhow ... Hope this is useful for you
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: genrating random numbers

Post 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. :)
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: genrating random numbers

Post by Stryks »

Hehehe ... nice solution ...

As always ... *wheres the 'not worthy' emoticon when you need it?*


8)
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: genrating random numbers

Post by swetha »

hey,i had already tried the option jedi mod has specified using the case statement.
gethinw
Forum Newbie
Posts: 16
Joined: Tue Sep 23, 2008 4:02 am

Re: genrating random numbers

Post 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...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: genrating random numbers

Post 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.
Post Reply