questions related to source code of PHP uniqid() [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
In PHP source code function uniqid() have following C code:
(I removed some types to shorten it)
//...
struct timeval tv;
gettimeofday(&tv, NULL);
int sec = (int) tv.tv_sec;
int usec = (int) (tv.tv_usec % 0x100000);
// The max value usec can have is 0xF423F,
// so we use only five hex digits for usecs.
printf("%08x%05x", sec, usec);
//...
If we put criticism aside, they try to produce 64 bit timestamp.
0xF423F is probably CLOCKS_PER_SEC - 1 (CLOCKS_PER_SEC is decimal 1000000),
but where this 0x100000 come from and what could be the reason to use modulus instead of bitwise and?

She or he could write the Unique ID as printf("%08x%08x", sec, usec)
sample output:
55189926000eb16f
5518997900051219
5518997a0005171b
The zeros in position 8 to 10 are consistent, they don't add entropy, so he wants to get rid of those zeros. The new UID will be 3 bytes shorter with the same entropy. He could simply use printf("%08x%05x", sec, usec);
sample output:
55189926eb16f
5518997951219
5518997a5171b
But that's on the assumption that usec is guaranteed to be less than 0x100000 otherwise UID will be up to 16 bytes long. You need % 0x100000 for insurance. It's also the same as & 0xFFFFF. Technically the insurance should be % 1000000 (decimal), but it doesn't really matter, it's still the same entropy.
Or we could just use the 16 byte version because saving 3 lousy bytes don't matter these days.

Related

How should I correct ill-formatted lat-long pairs to be valid for PHP computation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have access to a dataset that contains latitude and longitude pairs, but they are ill formatted and are not properly represented in the data set.
For example, a lat-long pair might look like this: 31333445, 105530865 when it should be 31.333445, -105.530865. Given the data set I am working with I know that the min value for latitude is 31.0 and the max is 37.0, and the min/max of longitude is -103 to -109.
If I was given a piece of paper and a pencil I could easily correct these myself, but the correction needs to happen on the fly when we receive input from a different program. We have no control over how the data is formatted until it hits our system and then we can make changes and corrections, and the lat-long pairs are all in a integer format listed above rather than a float.
What would be the best way to go about manually correcting this error? I am using PHP for our processing system.
If they're the same length then just divide by 1000000 and make negative where needed:
echo $lat / 1000000;
echo -$lon / 1000000;
If not then get the number of numbers at the start (2 and 3 here) making negative if needed, then insert a decimal and the remaining:
echo substr($lat, 0, 2) . '.' . substr($lat, 2);
echo -substr($lon, 0, 3) . '.' . substr($lon, 3);
You can use floatval() on the results if needed.
If the number of digits is always the same fixed size then use the solution suggested by #AbraCadaver in his comment ( just divide by 1000000 and multiply with -1)
If the number of digits can be different you need a different solution
and I have got a weird idea (at 0:24am)
I would convert the number to a string resulting in "31333445"
then concatenate "0." with the "31333445" resulting in "0.31333445"
then convert it back to a double resulting in 0.31333445
and then multiply it with 100 resulting in 31.333445 (and multiply the other value with -1 )
:-B
With this solution it does not matter if the number you get from outside has 3 or 14 digits
May sound weird but should work.
If this sounds to be a useful solution i will put into code tomorrow.

MySQL mod vs PHP mod [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Edited
Sorry guys I want PHP mod to display decimals
In MySQL select mod(15.6,1) result 0.6 but in PHP 15.6 % 1 result 0?
My purpose is to restrict calculation only for exact integer which does not allow decimals
In PHP, the % modulus operator works on integers. The operands 15.6 and 1 are cast to int (15 and 1) before being worked on. Therefore, 15 % 1, as you may expect, gives 0. Therefore "exact integer which does not allow decimals" matches the behaviour in PHP.
In MySQL, as the documentation says:
MOD() also works on values that have a fractional part and returns the exact remainder after division
Therefore, you should TRUNCATE(operand, 0) the operands before using them in MySQL to obtain the integer behaviour. This is consistent with PHP's behaviour of casting to int -- numbers are truncated, not floored (which is a significant difference with negative numbers).

How to get out number - php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to find out how I can extract $number out of the following code:
$calc = ($number % 10000) / 100;
So what's the opposite of %?
%, or the modulo operator, means "remainder after division by".
For example: 18 % 4 = 2, because if you divide 18 by 4 you get 4 with a remainder of 2. In the other direction, we can see that indeed 4 * 4 + 2 = 18.
Now consider the following:
18 % 4 = 2
22 % 4 = 2
26 % 4 = 2
...
2445678 % 4 = 2
As you can see, there are multiple values that are what we call congruent modulo 4. Therefore an inverse function of modulo cannot exist, because there is an infinite amount of possibilities.
Several cryptographic functions are based on the fact that the above holds true.
Generally you can't. % gives you the modulo of a division, there are mathematically an infinity of numbers that have the same modulo for a given ratio.
It is simply not bijective..

Combine Values to Create Bunch of 100 [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
suppose I have one array having the values:
array(20,40,30,15,60,50,10)
Now what i want is to I need to create bunch of 100 or near to hundred.and create separate round for each set of 100(or near to 100).
Say
Case 1:
Round 1: array(60,30,10) // 100 or near to 100
Round 2: array(40,50) // 100 or near to 100
Round 3: array(15,20) // 100 or near to 100 or remaining
Case 2:
Round 1: array(60,40) // 100 or near to 100
Round 2: array(50,20,30) // 100 or near to 100
Round 3: array(15,10) // 100 or near to 100 or remaining
So how can I achieve this?
Is there any algorithm regarding this that I can study?
You are describing the binpacking problem, which is NP-Complete, thus there is no known polynomial solution to solve it.
You can try an exponential approach (check all possibilities) if you need exact. If you want "close enough" you can search the literature for some approximation algorithm, or use some heuristic search solution from the AI field such as Genetic Algorithm or Hill Climbing.

How do make this calculation to achieve the answer I want? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is a bit of a puzzle. Look at the sum and result below:
$markTotal += ($session['Mark'] / 100 * $session['SessionWeight']);
Result on Browser:
Module: CHI2550 - Modern Database Applications 41.2 (this is $markTotal)
Session: AAB 72(this is $session['Mark']) 20% (this is $session['SessionWeight'])
Session: AAE 67(this is $session['Mark']) 40% (this is $session['SessionWeight'])
As you can see the answer to the calculation above is right, the answer is 41 .2 as it adds up the two session marks, divide by 100 and then times it by the total amount of the percentage.
But I want to include a total mark where except it is out of the total session percentage (60% for above example), it is out of a 100% but I can not work as simple as that as in above example one session is worth more than other. I have worked out that the answer for the total mark of the above example out of 100% should be 69, but how do I achieve this in my calculation.
Thank you and any help is much appreciated :)
You'd need to add up the total marks as well. So if session AAB has 90 total marks available (and the student got 72) and AAE has 80 marks (and got 67) then it'd be
(72) + (67) 14.4 26.8 41.2
(--) * 0.2 + (--) * 0.4 = ---- + ---- = ---- = 79.23%
(90) + (80) 18 34 52

Categories