Simple but puzzling question:
Say I have a string "516e965a8fe4b". I want it to become a number 0-100. Since there's far more than 100 possibilities of having an alphanumeric hash like that, overlaps are fine.
How do I go about implementing this?
I would love to know why you want this. Anyways this is how I would do it.
Add the ASCII values of each number or letter.
Then make a MOD 101 of the number. (Modulus)
ID= Sum % 101
Use something like this. Add the hex value of the numbers and mod it to 100:
function findNumber($hash) {
$sum=0;
for($i=0;$i<length($hash);$i++) {
$sum+=hexdec($hash[$i]);
}
return $sum%100;
}
function getNumber($string){
$value = 0;
for ($i=0; $i < strlen($string); $i++)
$value += hexdec($string[$i]);
$value = (int)($value/((strlen($string)+.001)*15/100));
return $value;
}
well, i have an alternative approach which is even SAFER than the others, because the result can't be directly determined by the input.
function getNumber($hex, $min, $max){
srand(hexdec($num));
return rand($min, $max);
}
You'll have a number between $min and $max (0 and 100 respectively in your case) which will be always the same every time you run this function with the same inputs (it's deterministic even if it uses random functions!)
Related
So i need to check if amount of chars from specific set in a string is higher than some number, what a fastest way to do that?
For example i have a long string "some text & some text & some text + a lot more + a lot more ... etc." and i need to check if there r more than 3 of next symbols: [&,.,+]. So when i encounter 4th occurrence of one of these chars i just need to return false, and stop the loop. So i think to create a simple function like that. But i wonder is there any native method in php to do such a thing? But i need some function which will not waste time parsing the string till the end, cuz the string may be pretty long. So i think regexp and functions like count_chars r not suited for that kind of job...
Any suggestions?
I don't know about a native method, I think count_chars is probably as close as you're going to get. However, rolling a custom solution would be relatively simple:
$str = 'your text here';
$chars = ['&', '.', '+'];
$count = [];
$length = strlen($str);
$limit = 3;
for ($i = 0; $i < $length; $i++) {
if (in_array($str[$i], $chars)) {
$count[$str[$i]] += 1;
if ($count[$str[$i]] > $limit) {
break;
}
}
}
Where the data is actually coming from might also make a difference. For example, if it's from a file then you could take advantage of fread's 2nd parameter to only read x number of bytes at a time within a while loop.
Finding the fastest way might be too broad of a question as PHP has a lot of string related functions; other solutions might use strstr, strpos, etc...
Not benchmarked the other solutions but http://php.net/manual/en/function.str-replace.php passing an array of options will be fast. There is an optional parameter which returns the count of replacements. Check that number
str_replace ( ['&','.','+'], '' , $subject , $count )
if ($count > $number ) {
Well, all my thoughts were wrong and my expectations were crushed by real tests. RegExp seems to work from 2 to 7 times faster (with different strings) than self-made function with simple symbol-checking loop.
The code:
// self-made function:
function chk_occurs($str,$chrs,$limit){
$r=false;
$count = 0;
$length = strlen($str);
for($i=0; $i<$length; $i++){
if(in_array($str[$i], $chrs)){
$count++;
if($count>$limit){
$r=true;
break;
}
}
}
return $r;
}
// RegExp i've used for tests:
preg_match('/([&\\.\\+]|[&\\.\\+][^&\\.\\+]+?){3,}?/',$str);
Of course it works faster because it's a single call to native function, but even same code wrapped into function works from 2 to ~4.8 times faster.
//RegExp wrapped into the function:
function chk_occurs_preg($str,$chrs,$limit){
$chrs=preg_quote($chrs);
return preg_match('/(['.$chrs.']|['.$chrs.'][^'.$chrs.']+?){'.$limit.',}?/',$str);
}
P.S. i wasn't bothered to check cpu-time, just was testing walltime measured via microtime(true); of the 200k iteration loop, but it's enough for me.
I'm generating a 6 digit code from the following characters. These will be used to stamp on stickers.
They will be generated in batches of 10k or less (before printing) and I don't envisage there will ever be more than 1-2 million total (probably much less).
After I generate the batches of codes, I'll check the MySQL database of existing codes to ensure there are no duplicates.
// exclude problem chars: B8G6I1l0OQDS5Z2
$characters = 'ACEFHJKMNPRTUVWXY4937';
$string = '';
for ($i = 0; $i < 6; $i++) {
$string .= $characters[rand(0, strlen($characters) - 1)];
}
return $string;
Is this a solid approach to generating the code?
How many possible permutations would there be? (6 Digit code from pool of 21 characters). Sorry math isn't my strong point
21^6 = 85766121 possibilities.
Using a DB and storing used values is bad. If you want to fake randomness you can use the following:
Reduce to 19 possible numbers and make use of the fact that groups of order p^k where p is an odd prime are always cyclic.
Take the group of order 7^19, using a generator co-prime to 7^19 (I'll pick 13^11, you can choose anything not divisible by 7).
Then the following works:
$previous = 0;
function generator($previous)
{
$generator = pow(13,11);
$modulus = pow(7,19); //int might be too small
$possibleChars = "ACEFHJKMNPRTUVWXY49";
$previous = ($previous + $generator) % $modulus;
$output='';
$temp = $previous;
for($i = 0; $i < 6; $i++) {
$output += $possibleChars[$temp % 19];
$temp = $temp / 19;
}
return $output;
}
It will cycle through all possible values and look a little random unless they go digging. An even safer alternative would be multiplicative groups but I forget my math already :(
There is a lot of possible combination with or without repetition so your logic would be sufficient
Collision would be frequent because you are using rand see str_shuffle and randomness.
Change rand to mt_rand
Use fast storage like memcached or redis not MySQL when checking
Total Possibility
21 ^ 6 = 85,766,121
85,766,121 should be ok , To add database to this generation try:
Example
$prifix = "stamp.";
$cache = new Memcache();
$cache->addserver("127.0.0.1");
$stamp = myRand(6);
while($cache->get($prifix . $stamp)) {
$stamp = myRand(6);
}
echo $stamp;
Function Used
function myRand($no, $str = "", $chr = 'ACEFHJKMNPRTUVWXY4937') {
$length = strlen($chr);
while($no --) {
$str .= $chr{mt_rand(0, $length- 1)};
}
return $str;
}
as Baba said generating a string on the fly will result in tons of collisions. the closer you will go to 80 millions already generated ones the harder it will became to get an available string
another solution could be to generate all possible combinations once, and store each of them in the database already, with some boolean column field that marks if a row/token is already used or not
then to get one of them
SELECT * FROM tokens WHERE tokenIsUsed = 0 ORDER BY RAND() LIMIT 0,1
and then mark it as already used
UPDATE tokens SET tokenIsUsed = 1 WHERE token = ...
You would have 21 ^ 6 codes = 85 766 121 ~ 85.8 million codes!
To generate them all (which would take some time), look at the selected answer to this question: algorithm that will take numbers or words and find all possible combinations.
I had the same problem, and I found very impressive open source solution:
http://www.hashids.org/php/
You can take and use it, also it's worth it to look in it's source code to understand what's happening under the hood.
Or... you can encode username+datetime in md5 and save to database, this for sure will generate an unique code ;)
How can I get maximum value of a function in specific range with php.
For example I have a function:
function f($x){
return pow($x,2);
}
and i want to get maximum value of this function in range (-1,1).
how can I implement this in php or using some other library?
There's no generic way to do this; you have to do the maths yourself.
Maximisation / Minimisation problems are solved by differentiation. In this case, you would get:
d(x^2)/dx = 2*x
The method for calculating the differential depends on your function. It's not that hard for simple functions like this, and Wolfram Alpha (http://www.wolframalpha.com/) will do it for you if you ask it nicely (http://www.wolframalpha.com/input/?i=d%28x%5E2%29%2Fdx).
Then you set that to 0, which tells you when the gradient is 0 (and therefore, it is at a maximum, minimum, or turning point):
2*x = 0
This tells you that you have a point to check at x = 0 (see the "solution" section here: http://www.wolframalpha.com/input/?i=d%28x%5E2%29%2Fdx%3D0). Now check the value of your function at the lower bound, upper bound, and the point(s) this tells you to check, and get the maximum/minimum of all those results. This will be your limit within that range.
$maximumValue = -999999999;
for ($i = -1; $i <= 1; $i++) {
$maximumValue = max($maximumValue, f($i));
}
var_dump($maximumValue);
..should work fine
This will only check for the numbers -1, 0 and 1. If you want more precision, just change $i++ to eg. $i += 0.1:
$maximumValue = -999999999;
for ($i = -1; $i <= 1; $i += 0.1) {
$maximumValue = max($maximumValue, f($i));
}
var_dump($maximumValue);
This will give you -1, -0.9, -0.8 ..... 0.8, 0.9 and 1.
It could also be changed to $i += 0.000000001 if you want to waste a lot of resources, but get a more accurate result.
If you use differentiation you have to deal with the case that there are no local optima. If the function is smooth this would mean that the optima occur on the endpoints. I am not sure how precise this all needs to be,but perhaps if the function is just given by a finite list of pairs you could sort a list which contains all the points where the function is defined.
I am trying to create a function that generates a random integer out of the bytes I get from /dev/urandom. I am doing this in PHP and it currently looks like:
public static function getRandomInteger($min, $max)
{
// First we need to determine how many bytes we need to construct $min-$max range.
$difference = $max-$min;
$bytesNeeded = ceil($difference/256);
$randomBytes = self::getRandomBytes($bytesNeeded);
// Let's sum up all bytes.
$sum = 0;
for ($a = 0; $a < $bytesNeeded; $a++)
$sum += ord($randomBytes[$a]);
// Make sure we don't push the limits.
$sum = $sum % ($difference);
return $sum + $min;
}
Everything works great except that I think it's not calculating the values exactly fair. For example, if you want to have a random value between 0 and 250, it receives one byte and mods it with 250 so the values of 0-6 are more likely to appear than the values of 7-250. What should I do to fix this?
a) If you don't need cryptographically secure random numbers, simply use mt_rand. It will probably suffice for your needs.
b) If you want to stick with your algorithm: Do some remapping: return round($min + $sum / pow(256, $bytesNeeded) * ($max - $min)).
c) As you can see, this requires rounding. That will lead to a not perfectly uniform distribution, I think (though I am not sure about this). Probably the best way is to get the random number as a float and then scale it. Though I have no idea how you get a float from /dev/urandom. That's why I stick with mt_rand and lcg_value.
I would read $difference bytes from /dev/urandom mod $difference and then add $min
Then make sure $max isn't higher than that number.
let's say I have a variable containing an integer or a float (since integers might overflow into a float in PHP).
I want to run some operation to get the leftmost digit and the rest of the remaining digits.
To explain better:
<?php
$x = NULL; //this will hold first digit
$num = 12345; //int
/// run operation
//outputs
//$x = 1;
//$num = 2345;
var_dump($x, $num);
?>
Now, I know there's multitudes of ways to do this if you represent the number as a string, but I'm trying to avoid type casting it into a string.
I'm probably looking for a solution which includes bitwise operations, but I'm pretty weak in that topic so I'm hoping someone who usually works low-level might be able to answer this!
Thanks a bunch.
I'm sure there is a way to do this without casting it to a string, but why? The string detour is so easy:
$x = (int)substr($num, 0, 1);
It'll give you a nice, proper integer.
Obviously, this does no extended checking for faulty input, and requires $num to be a valid number.
Avoids using any string manipulation, but no guarantees for float or even negative values
$x = NULL; //this will hold first digit
$num = 12345; //int
$m = 1;
while(true) {
$m *= 10;
if ($m > $num)
break;
}
$m /= 10;
$x = (int) floor($num / $m);
$num = $num % $m;
//outputs
//$x = 1;
//$num = 2345;
var_dump($x, $num);
Math-only method:
function leftMost($num) {
return floor($num/pow(10,(floor((log10($num))))));
}
explained I guess...
1+ log10 of num calculates the number of digits a number is, we floor it to remove any decimal values, put it as the exponent so for a 1 digit number we get 10^0=1, or a 8 digit number we get 10^8. We then are just divding 12345678/10000000 = 1.2345678, which gets floor'd and is just 1.
note: this works for numbers between zero and one also, where it will return the 2 in 0.02, and a string transform will fail.
If you want to work with negative numbers, make $num = abs($num) first.
To get the rest of the digits
$remainingnum = (int)substr((string)$num, 1, strlen($num));
If you typcast the value to a string you can use the array type selector.
For example:
$n = (string)12345676543.876543;
echo (int)$n[0];
#Mark Baker offered the best solution, though you should do abs(floor($num)) before applying the algorithm.
I know you stated you wanted to avoid casting to a string, but if you want to loop over the digits in PHP, this will be the fastest way:
$len = strlen($n);
for ($i = 0; $i < $len; ++$i)
$d = $n[$i];
In a quick-and-dirty benchmark, it was around 50% faster than the equivalent set of mathematical expressions, even when minimizing the calls to log and exp.