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();
Related
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.
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()
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 6 years ago.
Improve this question
I am trying to write a function that given an array like
var a = [
0.0015974718789698097
,0.755383581038094
,0.13950473043946954
,0.0011978091842754731
,0.005126875727346068
,0.0042250281407886295
,0.0001720958819913952
,0.0047584144830165875
,0.0835073272489086
,0.00016098907002300275
,0.0028037075787230655
,0.0014378579473690483
,0.00012411138102484662
]
or
var a = [
0.33333333333333333
,0.33333333333333333
,0.33333333333333333
]
or
var a = [
0.166666666666666
,0.166666666666666
,0.3
,0.3333333333333333
]
It round each number to 3 decimal places while keeping the sum of all the values is still equal to 1.0.
The way I imagine it would do this is by taking the difference of the new sum and the expected sum and distribute the difference while maintaining the relative distribution as close as possible. I can only think of an iterative approach and wanted to see what other solutions people can come up with
It's important to notice that the very last value of 0.00012411138102484662 will round to 0.000 but that doesn't it mean it should never get a piece of the difference, because the distribution it wants to maintain is the unrounded distribution, not the current distribution after rounding to 3 decimal places nor after iteration of balancing
If you are planning on rounding each of the values in the array to the 3rd decimal and adding them all up, Then here's what you need to do. You will need to get each number of the array into a loop and make that number to the fixed 3rd decimal by doing .toFixed(3) and parsing that to a float and adding it to a variable which will loop and the variable's value will change. Like this:
var int = 0;
a.forEach(number => { int += parseFloat(number.toFixed(3)) })
int // 1
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 6 years ago.
Improve this question
I have this code to add elements to my table and that part works perfectly fine. However, I want to add up the values of the last column in order to later print that on another part of the page. parseInt doesn't work for some reason (the values don't come back as integers and stay as strings, is there anything I have done wrong? Again, adding to the table works perfectly fine.
var hourC = 0;
var hourC =+ parseInt($(".hourCount").text(), 10);
alert(hourC);
Edit:
When I print the values of the variable hourC they don't add up to the previous value, they just stay next to each other. Example: 1 + 1 = 11 rather than 2. I don't see where my issue is and the answer for debugging didn't help since I still got the same result.
Final Edit:
I achieved what I wanted now through a different medium, I created an array and pushed the values into the array and then I used "join" to solve the issue.
If interested in what I was asking for here is a fiddle with the final result. (You can just change the console.log to alert)
Basic debugging skills.
parseInt works -- its a well tested function. If it broke in a browser, a lot of people would notice.
However, you haven't given any way to figure out what is going on with your code, since the real problem MUST be here:
$(".hourCount").text()
That must not contain whatever value you think it does.
Split your code up and use the debugger, or even console log, to see what the values are.
var hourC = 0;
var strValue = $(".hourCount").text();
alert(["strValue = ", strValue]);
hourC = parseInt(strValue, 10);
alert(hourC);
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 8 years ago.
Improve this question
So , i have a problem . i am calculating the discount and it may be in decimal points . the method i am using is not working for the value 0.0038575 and the method is
value=0.0038575
Math.round(value * 100) / 100,
it is returning the same value . this method is working for all other values . but the expected result is 0.004
now i changed the method i used this one var discount = 0.0038575.toFixed(3); working fine but the issue with this is it is not working if i pass the variable like below
var value=0.0038575;
var discount = value.toFixed(3);
error is value.toFixed is not a functioni also tried this one for solve
var value=0.0038575;
var discount = "'"+value+"'".toFixed(3);
any help will be appreciated . thanx in advance
Following snippet works for me:
var value = 0.0038575;
var discount = value.toFixed(3);
console.log(discount);
If your value is of type string you can try to cast it to a number with the + operator:
var discount = (+value).toFixed(3);
But without any reproducible code snippet I can only guess an answer.
this worked for me
var value = 0.0038575;
var loss = parseFloat(value).toFixed(3);