A Bigger Random Number After Refreshing Page [closed] - javascript

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 5 years ago.
Improve this question
I'm using some javascript in my new web project. I need too generate a random number. But it's a little different !
The code should generate a bigger random number after any refresh !
Fore example :
First it gives : 1000
After refresh : 2000
Another refresh : 4302
And ...
I mean the code should never give us, any repeated number and after every single refresh, the number must be bigger than the last !
Is there any method for javascript?! Or I need to use other things like PHP or ...
If yes, How ?!

use new Date().valueOf() (representing the current timestramp in milliseconds) it will be always bigger than the previous because it is unidirectional

You could accomplish that this using a PHP session (since you have included PHP in your tag, I'm assuming this is OK), and then set it as a global JS variable that you can access:
<?php
session_start();
if (isset($_SESSION['random_number'])) {
$_SESSION['random_number'] += rand();
} else {
$_SESSION['random_number'] = rand();
}
?>
<script type="text/javascript">
var random_number = <?php echo $_SESSION['random_number']; ?>;
</script>

If you're using PHP, you can store it in a session. Then just generate a random number with the rand() function, using min parameter to ensure you get a higher number each refresh. Passing the PHP_INT_MAX constant to the max parameter, you will get a "highest" possible number the PHP engine can handle.
By adding $_SESSION['random_integer'] + 1 to min if the session is set already, we guarantee that the next random number will be higher than the current one. If it's not set, we just define it to be zero.
session_start();
$min_value = !isset($_SESSION['random_integer'] ? 1 : $_SESSION['random_integer'] + 1;
$_SESSION['random_integer'] = rand($min_value, PHP_INT_MAX);
echo $_SESSION['random_integer'];
PHP_INT_MAX will differ from 32 and 64 bit systems, but it should be sufficiently high enough.
PHP manual on rand()

Related

Generation of random numbers - Math.random() [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 14 days ago.
This post was edited and submitted for review 14 days ago.
Improve this question
I was working trying to generate a random number the other day. The assignment is basically that, given an array with names, I was gonna log names on the console at random. I tried to generate a random number that is gonna help accessing the names working as the index of the elements (e.g., array[random_number_as_index]).
My confusion started when storing the method Math.random() in a variable and accessing that same variable, the result was the exact same number over and over. My line of code looks something like this.
const randomNumber = Math.floor(Math.random()*10));
Whereas when part of a function, e.g.,
function randomNum () {return Math.floor(Math.random()*10));}
would actually generate a random number every time the function is invoked. My expectation was that, upon accessing the variable, a random number was gonna be generated every single time, which was not the case, and only works if it is within a function.
const randomNumber = Math.floor(Math.random()*10));
Generates a random number, then assigns that random number to randomNumber.
randomNumber is a constant variable (meaning its value cannot be changed after the fact).
You're generating one random number, then saving it in a variable.
function randomNum () {return Math.floor(Math.random()*10));}
Defines a function that returns a random number when called.
Each invocation of randomNum generates a new random number.

Reading HTML file 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 5 years ago.
Improve this question
I am currently a Student, developing a Website for an Online Course Planning for Monash I am Stuck with a reading HTML files in my PHP code
I need to Upload a File (Unofficial Records of a Student) and once I Click Upload, the System MUST Scan the HTML file and read through the Tables from the first (of the academic Records to the Last one) where it says Incomplete and it must evaluate the Grade, if it is a P, C, D, HD, the System will automatically Cross that unit for the Student, if the Student has Failed, it will be a N and the System must automatically highlight the Unit in order to tell that the student needs to re do the Unit
when the Academic Record is saved from the official Monash website, it saves automatically as a HTML, so i have no way in changing that
(Note that it is a student who will download in future use, not programmers, so they might not know how to convert it)
Like so - to read the file?
<?php
$text= file_get_contents('yourfile.htm');
echo $text;
?>
Once you have it grabbed, you can parse the HTML - see here:
PHP Parse HTML code
You could load the HTML into a DOMDocument and then iterate over the document nodes to extract the information.
A quick example from the top of my head (did not test it thought):
$document = new DOMDocument();
$document->loadHTMLFile("path-to-your-html-file.html");
$tableElement = $document->getElementById("your-table-id");
$allTableRows = $tableElement->getElementsByTagName("tr");
foreach($allTableRows as $tableRow) {
$allTableCellsInThisRow = $tableRow->getElementsByTagName("td");
$firstCell = $allTableCellsInThisRow->item(0);
$secondCell = $allTableCellsInThisRow->item(1);
// and so on ..
// do your processing of table rows and data here
}
Links
http://php.net/manual/de/domdocument.loadhtmlfile.php
http://php.net/manual/de/class.domnodelist.php
You can try this piece of code:
https://www.w3schools.com/php/func_string_money_format.asp
Here
<?php
$file = fopen("test.html","r");
echo fgets($file);
fclose($file);
?>
OR
Try:
readfile("/path/to/file");
OR
echo file_get_contents("/path/to/file");

Math method gives me the same number all over the time [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Recently i've been trying to do a repetitive random number in JS, but the problem is that always i have to reload the page to get a new random number, even i try to show it multiple times but the problem is that the number is the same until i refresh the page.
<?php
<script type="text/javascript" charset="utf-8">
function getNumber(){
var number = Math.random()
return number;
}
document.write(getNumber()); // NUMBER 1
document.write(getNumber()); // SAME AS NUMBER 1
document.write(getNumber()); // SAME AS NUMBER 1
document.write(getNumber()); // SAME AS NUMBER 1
document.write(getNumber()); // SAME AS NUMBER 1
</script>
?>
I think it might be because you are using document.write. Use console.log instead and take a look at the console.
console.log(getNumber());
console.log(getNumber());
If you want your results in the browser get a reference to an element, or make and append one.
Math.random() returns a number between 0 and 1. Please read the docs for more information.
Rather than using document.write just update the content of your element like this:
function getNumber(){
var number = Math.random()
return number;
}
var div = document.getElementById('result');
div.innerHTML = getNumber();

Is it possible to be able to increment a particular column value (int) by one via php? [duplicate]

This question already has answers here:
Increment a database field by 1
(5 answers)
Closed 8 years ago.
I have a button on a page that I would like use to increase the value of a column in a particular row of my database by 1. I have an idea of how I will parse the info except not entirely 100% on the sql e within my 'bridging' php file.
I'm thinking it will be an update statement but I'm not sure whether I can use i++ or something similar to increase the values by one.
$result = mysql_query("UPDATE markers SET verification = "???" WHERE name="somename")
The system I'm trying to build is a bit of an upvote/downvote system which the current 'numbervalue' is stored in the db, for use elsewhere within the page.
How can I do this? Cheers
Yes you could increment them by one using your query with this:
$result = mysql_query("UPDATE markers SET verification = verification + 1 WHERE name='somename'");
Sidenote: And if possible, use a newer API which is mysqli or PDO instead.

Point Spending Tool - Difficulties [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 9 years ago.
Improve this question
I've made this small tool but I'm not sure how to achieve what I need.
I would like it to subtract from the "Points Left" when adding to the others.
(Not going below zero into the negative, only 30 points).
I would also like to prevent going BELOW the initial numbers in "Weapon Power" and "Magic Power".
(I would like to be able to only spend a maximum of 25 points into one "power")
I think it kind of explains itself so maybe I'm just confusing you more.
Any ideas?
DEMO
You need to give your number input elements distinct ids, because when you do document.getElementById, it will only return you the first element with the given ids.
Then, you need to give another distinct ID to the "points left" field for each character, and update that one. To do that, you'll need to pass to add and substract the correct value ( add(warrior, 'weapon'), add(wizard, 'magic')).
You need to be able to get your IDs from your warrior or wizard object, so you could try doing this:
var wizard = {
weapon:"idOfWeaponField", magic:"idOfMagicField", points:"idOfPointsField"
};
where the string values are the ids of your elements.
Then, within your add function, you can access your id like this
function add(character, statName){
var myID = character[statName];
}
and update the correct input value.
EDIT: code here.
I would also like to prevent going BELOW the initial numbers in "Weapon Power" and "Magic Power". >(I would like to be able to only spend a maximum of 25 points into one "power")
Basically, you've seen what I did with the points left, when I said if(pointsVal.value == 0) return; ? You should be able to implement any constraint you like using that.
I modified mine to give an example where the limits are at their initial values.
If you only want to spend a given number of points on one of the powers, you'll have to substract the limit from the current number and return from add() without changing the value if the number exceeds the threshold. That should be pretty easy with what you have now, since you don't have to change the character object to do it.

Categories

Resources