Code

+2 william appiah-dankwah · June 30, 2014
whats wrong with this code I'm trying to do Math.random()

should it be?

function Code(){

var flow = 10

document.write(Math.random() + flow);
}

or should it be?

function Code(){

var flow = 10

Math.random(flow);
}

Post a Reply

Replies

- page 2
Oldest  Newest  Rating
0 william appiah-dankwah · July 1, 2014
o ill start using them its straight from my editor it don't have tags
0 Declan Hester · July 1, 2014
Please note, this advice comes from not knowing what you're trying to do in the grand scheme of things, so it may be completely inappropriate for future needs, but should produce your desired outcome for the purposes you mentioned.



function Code(){
var flow = 10;
for(var i=0; i<5;i++){
document.write(Math.floor(Math.random() * flow)+1);
}
}


Math.floor(x) will always return the nearest integer that is equal to or less than x. So if x were 2.0 it would return 2.0, if x were 3.4825 it would return 3.0 Effectively taking the lowest point (the floor) of the integer.

You could also use Math.round(y) for a whole number, but because of the rules regarding rounding (.0 to .4 round down, .5 to .9 round up) the resulting integer may not always be the same (for example, if y is 3.2 the result would be rounded to 3.0, but if y was 3.5 the result would be rounded to 4.0) and this may not always be ideal.
0 william appiah-dankwah · July 16, 2014
thanks for the positive motivation comments smurfing sounds cool the name might stick
0 william appiah-dankwah · July 19, 2014
function randomString(){


var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for(var i=0; i < string_length; i++){

   var rnum = Math.floor(Math.random() * chars.length);
   randomstring += chars.substring(rnum,rnum+1);
}

 document.randform.randomfield.value = randomstring;
}

dose anyone know were (document.randformrandomfield.value) comes from trying to work out this part of the code
0 william appiah-dankwah · July 2, 2014
is this random array generator correct?

1. function Gold(){

2. var plan = new Array

3. plan[0] = "william"
4. plan[1] = "gus"
5. plan[2] = "kuz"
6. plan[3] = "jim"
7. plan[4] = "cant"

8. for(i = 0; i < plan.length; i++){

9. document.write(Math.floor(Math.random() * plan.length));
10. }
11. }

12. Gold();
-1 william appiah-dankwah · July 1, 2014
is this correct?

1. function Grade(){

2. var Blue = 6

3. document.write(Math.floor(Math.random() * Blue)+1);

4. }

5. Grade();
  • 1
  • 2

Javascript

129,743 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators