Skip to main content
Code Review

Return to Answer

replaced http://us1.php.net with https://www.php.net
Source Link
  • Use chr($randInt) instead of $charUniverse[$randInt], and abandon the $charUniverse variable altogether.
  • Use the $str .= "str" dot-equals operator in PHP to append to your final string.

Thus your code becomes:

public function generateKey($size){
 
 $randomString = "";
 $charDomainSize = 256;
 for($i=0;$i<$size;$i++){
 $randInt=mt_rand(0, $charDomainSize-1);
 $randChar=chr($randInt);
 $randomString .= $randChar;
 }
 return $randomString; 
 
}

If we compress your code further, it becomes:

public function generateKey($size){
 $randomString = "";
 for($i=0;$i<$size;$i++){
 $randomString .= chr(mt_rand(0, 255));
 }
 return $randomString;
}

I conducted a speed test, as well. The new code is faster (1000 runs, varied sizes between 1128 and 1148). The units are seconds.

  • Function 1 Average: 0.024438648223877
  • Function 2 Average: 0.015253195762634

Only using 256 as a size, 1000 runs:

  • Function 1 Average: 0.00031177306175232
  • Function 2 Average: 0.00017282342910767

This is a little overkill, but I was bored.

It should be very easy to port your random string generator to other platforms, although it may develop more or less dependable randomness based on the underlying generators. E.g. don't use Java's java.util.Random for security or scientific purposes (see Difference between java.util.Random and java.security.SecureRandom).

You might also want to consider PHP's openssl_random_pseudo_bytes openssl_random_pseudo_bytes, depending on your intent. That may not be as easily ported, though, as your current code.

  • Use chr($randInt) instead of $charUniverse[$randInt], and abandon the $charUniverse variable altogether.
  • Use the $str .= "str" dot-equals operator in PHP to append to your final string.

Thus your code becomes:

public function generateKey($size){
 
 $randomString = "";
 $charDomainSize = 256;
 for($i=0;$i<$size;$i++){
 $randInt=mt_rand(0, $charDomainSize-1);
 $randChar=chr($randInt);
 $randomString .= $randChar;
 }
 return $randomString; 
 
}

If we compress your code further, it becomes:

public function generateKey($size){
 $randomString = "";
 for($i=0;$i<$size;$i++){
 $randomString .= chr(mt_rand(0, 255));
 }
 return $randomString;
}

I conducted a speed test, as well. The new code is faster (1000 runs, varied sizes between 1128 and 1148). The units are seconds.

  • Function 1 Average: 0.024438648223877
  • Function 2 Average: 0.015253195762634

Only using 256 as a size, 1000 runs:

  • Function 1 Average: 0.00031177306175232
  • Function 2 Average: 0.00017282342910767

This is a little overkill, but I was bored.

It should be very easy to port your random string generator to other platforms, although it may develop more or less dependable randomness based on the underlying generators. E.g. don't use Java's java.util.Random for security or scientific purposes (see Difference between java.util.Random and java.security.SecureRandom).

You might also want to consider PHP's openssl_random_pseudo_bytes, depending on your intent. That may not be as easily ported, though, as your current code.

  • Use chr($randInt) instead of $charUniverse[$randInt], and abandon the $charUniverse variable altogether.
  • Use the $str .= "str" dot-equals operator in PHP to append to your final string.

Thus your code becomes:

public function generateKey($size){
 
 $randomString = "";
 $charDomainSize = 256;
 for($i=0;$i<$size;$i++){
 $randInt=mt_rand(0, $charDomainSize-1);
 $randChar=chr($randInt);
 $randomString .= $randChar;
 }
 return $randomString; 
 
}

If we compress your code further, it becomes:

public function generateKey($size){
 $randomString = "";
 for($i=0;$i<$size;$i++){
 $randomString .= chr(mt_rand(0, 255));
 }
 return $randomString;
}

I conducted a speed test, as well. The new code is faster (1000 runs, varied sizes between 1128 and 1148). The units are seconds.

  • Function 1 Average: 0.024438648223877
  • Function 2 Average: 0.015253195762634

Only using 256 as a size, 1000 runs:

  • Function 1 Average: 0.00031177306175232
  • Function 2 Average: 0.00017282342910767

This is a little overkill, but I was bored.

It should be very easy to port your random string generator to other platforms, although it may develop more or less dependable randomness based on the underlying generators. E.g. don't use Java's java.util.Random for security or scientific purposes (see Difference between java.util.Random and java.security.SecureRandom).

You might also want to consider PHP's openssl_random_pseudo_bytes, depending on your intent. That may not be as easily ported, though, as your current code.

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  • Use chr($randInt) instead of $charUniverse[$randInt], and abandon the $charUniverse variable altogether.
  • Use the $str .= "str" dot-equals operator in PHP to append to your final string.

Thus your code becomes:

public function generateKey($size){
 
 $randomString = "";
 $charDomainSize = 256;
 for($i=0;$i<$size;$i++){
 $randInt=mt_rand(0, $charDomainSize-1);
 $randChar=chr($randInt);
 $randomString .= $randChar;
 }
 return $randomString; 
 
}

If we compress your code further, it becomes:

public function generateKey($size){
 $randomString = "";
 for($i=0;$i<$size;$i++){
 $randomString .= chr(mt_rand(0, 255));
 }
 return $randomString;
}

I conducted a speed test, as well. The new code is faster (1000 runs, varied sizes between 1128 and 1148). The units are seconds.

  • Function 1 Average: 0.024438648223877
  • Function 2 Average: 0.015253195762634

Only using 256 as a size, 1000 runs:

  • Function 1 Average: 0.00031177306175232
  • Function 2 Average: 0.00017282342910767

This is a little overkill, but I was bored.

It should be very easy to port your random string generator to other platforms, although it may develop more or less dependable randomness based on the underlying generators. E.g. don't use Java's java.util.Random for security or scientific purposes (see Difference between java.util.Random and java.security.SecureRandom Difference between java.util.Random and java.security.SecureRandom).

You might also want to consider PHP's openssl_random_pseudo_bytes, depending on your intent. That may not be as easily ported, though, as your current code.

  • Use chr($randInt) instead of $charUniverse[$randInt], and abandon the $charUniverse variable altogether.
  • Use the $str .= "str" dot-equals operator in PHP to append to your final string.

Thus your code becomes:

public function generateKey($size){
 
 $randomString = "";
 $charDomainSize = 256;
 for($i=0;$i<$size;$i++){
 $randInt=mt_rand(0, $charDomainSize-1);
 $randChar=chr($randInt);
 $randomString .= $randChar;
 }
 return $randomString; 
 
}

If we compress your code further, it becomes:

public function generateKey($size){
 $randomString = "";
 for($i=0;$i<$size;$i++){
 $randomString .= chr(mt_rand(0, 255));
 }
 return $randomString;
}

I conducted a speed test, as well. The new code is faster (1000 runs, varied sizes between 1128 and 1148). The units are seconds.

  • Function 1 Average: 0.024438648223877
  • Function 2 Average: 0.015253195762634

Only using 256 as a size, 1000 runs:

  • Function 1 Average: 0.00031177306175232
  • Function 2 Average: 0.00017282342910767

This is a little overkill, but I was bored.

It should be very easy to port your random string generator to other platforms, although it may develop more or less dependable randomness based on the underlying generators. E.g. don't use Java's java.util.Random for security or scientific purposes (see Difference between java.util.Random and java.security.SecureRandom).

You might also want to consider PHP's openssl_random_pseudo_bytes, depending on your intent. That may not be as easily ported, though, as your current code.

  • Use chr($randInt) instead of $charUniverse[$randInt], and abandon the $charUniverse variable altogether.
  • Use the $str .= "str" dot-equals operator in PHP to append to your final string.

Thus your code becomes:

public function generateKey($size){
 
 $randomString = "";
 $charDomainSize = 256;
 for($i=0;$i<$size;$i++){
 $randInt=mt_rand(0, $charDomainSize-1);
 $randChar=chr($randInt);
 $randomString .= $randChar;
 }
 return $randomString; 
 
}

If we compress your code further, it becomes:

public function generateKey($size){
 $randomString = "";
 for($i=0;$i<$size;$i++){
 $randomString .= chr(mt_rand(0, 255));
 }
 return $randomString;
}

I conducted a speed test, as well. The new code is faster (1000 runs, varied sizes between 1128 and 1148). The units are seconds.

  • Function 1 Average: 0.024438648223877
  • Function 2 Average: 0.015253195762634

Only using 256 as a size, 1000 runs:

  • Function 1 Average: 0.00031177306175232
  • Function 2 Average: 0.00017282342910767

This is a little overkill, but I was bored.

It should be very easy to port your random string generator to other platforms, although it may develop more or less dependable randomness based on the underlying generators. E.g. don't use Java's java.util.Random for security or scientific purposes (see Difference between java.util.Random and java.security.SecureRandom).

You might also want to consider PHP's openssl_random_pseudo_bytes, depending on your intent. That may not be as easily ported, though, as your current code.

Source Link
  • Use chr($randInt) instead of $charUniverse[$randInt], and abandon the $charUniverse variable altogether.
  • Use the $str .= "str" dot-equals operator in PHP to append to your final string.

Thus your code becomes:

public function generateKey($size){
 
 $randomString = "";
 $charDomainSize = 256;
 for($i=0;$i<$size;$i++){
 $randInt=mt_rand(0, $charDomainSize-1);
 $randChar=chr($randInt);
 $randomString .= $randChar;
 }
 return $randomString; 
 
}

If we compress your code further, it becomes:

public function generateKey($size){
 $randomString = "";
 for($i=0;$i<$size;$i++){
 $randomString .= chr(mt_rand(0, 255));
 }
 return $randomString;
}

I conducted a speed test, as well. The new code is faster (1000 runs, varied sizes between 1128 and 1148). The units are seconds.

  • Function 1 Average: 0.024438648223877
  • Function 2 Average: 0.015253195762634

Only using 256 as a size, 1000 runs:

  • Function 1 Average: 0.00031177306175232
  • Function 2 Average: 0.00017282342910767

This is a little overkill, but I was bored.

It should be very easy to port your random string generator to other platforms, although it may develop more or less dependable randomness based on the underlying generators. E.g. don't use Java's java.util.Random for security or scientific purposes (see Difference between java.util.Random and java.security.SecureRandom).

You might also want to consider PHP's openssl_random_pseudo_bytes, depending on your intent. That may not be as easily ported, though, as your current code.

lang-php

AltStyle によって変換されたページ (->オリジナル) /