Random number every week for all visitors [closed] - php

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 6 years ago.
Improve this question
I want to show a random number between 1,000 and 1,999 with these requirements
Show a new number every Monday
With comma for thousands separator
Show the same number to all visitors
What I found so far:
<?php
srand(floor(date('U')) / (60*60*24*7));
$num = 100;
$a = rand() % $num;
echo $a;
echo "<br>";
$b = rand() % $num;
echo $b;
echo "<br>";
$c = rand() % $num;
echo $c;
echo "<br>";
$d = rand() % $num;
echo $d;
echo "<br>";
$e = rand() % $num;
echo $e;
?>

By creating an appropriate seed you can do this without needing to store the result.
You'll want to get the ISO-8601 week-numbering year and the week number. You can generate this with date('oW'). Today (and until next Monday) that'll return 201638.
The value out of W changes every Monday and the value from o changes every year unless the current day's W belongs to the prior or next year - in which case that year is used. (In other words the year/week combo will never change in the middle of the week due to the new year.)
Once you have that combo use it to seed your random number generator:
mt_srand((int)date('oW'));
Then pull your random number between your limits. With the fixed seed this'll produce the same value for each of your visitors:
$number = mt_rand(1000, 1999);
Then format it to add the thousands separator and output:
echo number_format($number);
All together, skipping the intermediate variable:
mt_srand((int)date('oW'));
echo number_format(mt_rand(1000, 1999));
Today this outputs 1,331.
There's no need to store the generated number anywhere as this reliably regenerates the same number for every visitor, every day until the next Monday - at which point you get a new seed and therefore a new random number (until the following Monday, as so on.)
For more information on the functions used see the PHP manual:
date()
mt_srand()
mt_rand()
number_format()

Related

How to get the numbers after the decimal place for an if statement in php [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 7 years ago.
Improve this question
I am just trying to figure out how I can get the actual digits of a figure that has been calculated within php and formatted to have 2 decimal places.
so say its calculated it to be 45.76 I am trying to figure out how I can get the 76 from it for an if statement. Basically I want it to look and just say that if it's 00 then remove them, if not, show them.
Thanks
Try :
function showDecimals($v){
$n = abs($v);
$whole = floor($n);
$fraction = $n - $whole;
return $fraction > 0
}
And...
if (showDecimals(10.15)){
//Show
}else{
//Remove?
}
You want to show a whole number if there is no decimal place, and 2 decimals of precision if not?
Method 1
function formatNumber($n) {
$n = round($n*100)/100;
return ''+$n;
}
This simply rounds it to 2 decimals of precision. Zero truncation is automatic.
Usage
echo formatNumber(0); //0
echo formatNumber(0.5); //0.5
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.9
echo formatNumber(1.896); //1.9
Method 2
Or if you 1.9 to display as 1.90, I suppose this would work:
function formatNumber($n) {
if ($n == 0)
return ''+$n;
$str = ''.round($n*100)/100;
$dotpos = strrpos('.', $str);
if (strlen(substr($str, $dotpos+1)) === 2)
$str .= '0';
return $str;
}
Usage:
echo formatNumber(0); //0
echo formatNumber(0.5); //0.50
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.90
echo formatNumber(1.896); //1.90
Edit: Accidentally posted broken version of method 2, should be fixed now.

How to count complete sets of items in 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 7 years ago.
Improve this question
I am a newbie, so hopefully I word this properly. I have searched and searched both SO and the web to try to find this answer, even mathematically, to understand how to code it, but to no avail.
On a web game, I want to be able to count the number of complete sets a user has (the set has four items).
For example, the user can attain turkey, pumpkin pie, stuffing, and mashed potatoes. Those are displayed in a table by quantity, like so:
Mashed Potatoes: 2
Turkey: 4
Pumpkin Pie: 4
Stuffing: 3
So in this case, the user has 2 total sets of what I'll call a "Thanksgiving Dinner." The Thanksgiving Dinner must include one of each item to be a complete set.
How can I use PHP to count and display the number of complete sets the user has?
This feels like it should be so simple (mathematically, at least), but I am really struggling.
Try this, simple, as shown in this PHP Fiddle
<?php
$min = 100000; // just initialize min with large value.
// these values can be hard coded, from JSON, html form, or database
// it is just written for illustration
$arr = array(
'Mashed Potatoes' => 2,
'Turkey' => 4,
'Pumpkin Pie'=> 4,
'Stuffing'=> 3
);
// we loop through each element of the above array and,
// compare its value to the min.
foreach($arr as $value){
if( $value < $min){
// If the value is less that min. then we store value into min
$min = $value;
}
}
echo 'You can have ' . $min . ' Thanksgiving dinners';
?>
EDIT:
As suggested in a comment from PaparazzoKi, Thanks, you can replace all of this:
foreach($arr as $value){
if( $value < $min){
// If the value is less that min. then we store value into min
$min = $value;
}
}
echo 'You can have ' . $min . ' Thanksgiving dinners';
With this line:
echo 'You can have ' . min($arr) . ' Thanksgiving dinners';
Making use of the .min() function
The total number of complete set will the the key with the lowest value in the array.
You may use the min() function of PHP
http://php.net/manual/en/function.min.php

Extract one digit from every four digit in a sixteen digit integer and store in a variable in 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 7 years ago.
Improve this question
I have generate a 16 digit number and i want to extract one number from every 4 digits of that 16 digit number. For e.g: 1234567892345678. I want to extract 2 from 1234, 7 from 5678, 3 from 9034 & 7 from 5678. Then store it in another variable $a. the extraction will be in a random manner.
You can try this -
$d = '1234567892345678';
$s = str_split($d, 4); // split in 4 digits
$n = array_map(function($x) {
return substr($x, rand(0, 3), rand(1, 1)); // extract single digit random number
}, $s);
$n will hold the random numbers.
I might be late at answering this question but you can simply use strlen function along with for loop like as
$str = "1234567892345678";
for($i = 0; $i < strlen($str);$i += 4){
echo $str[$i+rand(0,3)];
}
Here you have a one-liner:
$s = $string[rand(0,3)].$string[rand(4,7)].$string[rand(8,11)].$string[rand(12,15)];
echo $s;
Another one-liner:
for($s='',$i=0;$i<10; $s.=$string[rand($i,$i+=3)]);
echo $s;

PHP loop (for each day of a week and each hour) [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 7 years ago.
Improve this question
I want to create a loop (the result will be inserted into javascript)
Syntax:
The first number is the day (Monday - Friday/1 - 5)
The second number is the hour (1 - 11)
So this is finally what the loop should return:
subject_1_1: $("#subject_1_1").val(),
subject_2_1: $("#subject_2_1").val(),
subject_3_1: $("#subject_3_1").val(),
subject_4_1: $("#subject_4_1").val(),
subject_5_1: $("#subject_5_1").val(),
subject_1_2: $("#subject_1_2").val(),
subject_2_2: $("#subject_2_2").val(),
subject_3_2: $("#subject_3_2").val(),
subject_4_2: $("#subject_4_2").val(),
subject_5_2: $("#subject_5_2").val(),
subject_1_3: $("#subject_1_3").val(),
until
subject_5_11: $("#subject_5_11").val()
The last element shouldn't have a comma.
// first sets x = 1, then, while x isn't equal to 5 the code
// inside `{ ... }` is executed and x is added 1
for($x=1;$x<=5;$x++){
// again, but this time with y, from 1 to 11.
for($y=1;$y<=11;$y++){
// So we end up here and we now this code is going to execute
// elevent times for each x value, and a total of 5 x values.
// thats (x = from 1 to 5) * (y = from 1 to 11).
// This string is printed (`echo`) every time for each iteration.
echo "subject_1_3: $(\"#subject_".$x."_".$y."\").val()".(!($x==5&&$y==11)?",":"")."\n";
}
}
Edited 1: now removes last comma.
Edited 2: added comments, but read:
In the string being printed, I notice this code:
!($x==5&&$y==11)?",":""
That does the following:
if this is true ? this is executed : and if it is not, then this is executed
So, in pseudo:
(if not (x=5 and y = 11)) then (print comma) else (don't)
Why not do it in the JavaScript itself?
var obj, x, y;
obj = {}; // this will be the object your subject_X_Y properties belong to
for( x=1; x<=5; x++) {
for( y=1; y<=11; y++) {
obj['subject_'+x+'_'+y] = $('#subject_'+x+'_'+y).val();
}
}

random number generate in 5 minutes

How can I generate a random number in every 5 minutes? When I searched for it, I found Need to generate random number after time interval. Using that, I coded like,
<?
$seed = floor(time()/(60*60*12));
srand($seed);
$item = rand(0,9);
echo $item;
?>
But the value of $item is not changing in 5 minute. How can I edit this code?
The example you pulled from the other SO question generates a new random number every TWELVE HOURS, as indicated by the "12" in your code.
Since you want a new random number every FIVE MINUTES, the code is as follows:
<?
$seed = floor(time()/(60*5));
srand($seed);
$item = rand(0,9);
echo $item;
?>
You seed it with the same value for every 12 hour period, which causes it to show the same result. Stop seeding unnecessarily.

Categories