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 1
Oldest  Newest  Rating
+1 Franz Schmidt · July 12, 2014
That code doesn't seem useful: The switch statement checks the value of the randomnumber and in case of your options (e.g. case 0: ) you just say 0 that has no effect...
If you just want to echo the number you just need the document.write(Num):

function Cold(){
var Num = Math.floor(Math.random() * 10);
document.write(Num);
}
Cold();

+1 Franz Schmidt · July 13, 2014
If you want to generate names you need a list with names which can be used.
There you could use the switch.
For instance:

function Cold(){
var Num = Math.floor(Math.random() * 10);
var Name;
switch(Num){
case 0: Name='Bucky';
break;
case 1: Name='Dave';
break;
case 2: Name='Tina';
break;
case 3: Name='Fred';
break;
.
.
.
}
document.write(Name);
}
Cold();



+1 Hossein Vahabi · July 5, 2014
if you wanna some rand numbers ... it's your answer :


function rand(){
var rand = Math.floor(Math.random() * 10);
}


* then use a switch or if (conditional statement) to get numbers in random from rand variable. it's should be between 0-9.
0 william appiah-dankwah · July 12, 2014

function Cold(){

var Num = Math.floor(Math.random() * 10);

switch(Num){

case 0: 0;
case 1: 1;
case 2: 2;
case 3: 3;
case 4: 4;
case 5: 5;
case 6: 6;
case 7: 7;
case 8: 8;
case 9: 9;

}

document.write(Num);

}

Cold();


is that right with a switch works but not sure if its couse of the switch or not
0 william appiah-dankwah · July 12, 2014
thats not what I'm looking for I'm trying to randomly generate names?
0 william appiah-dankwah · July 14, 2014
i can't work out the array didn't i start with that llama?
0 william appiah-dankwah · June 30, 2014
just messing around with the Math.random code i want to see how it affects the variable i have created also i might try a loop 
0 william appiah-dankwah · July 15, 2014
check the second page of this thread llama
0 william appiah-dankwah · July 1, 2014
this is the code i have written but i am looking for whole numbers 

function Code(){

var flow = 10

for(var i = 0; i < 5; i++){


document.write(Math.random() * flow);


}
}
0 william appiah-dankwah · July 1, 2014
what tags?
  • 1
  • 2

Javascript

107,325 followers
About

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

Links
Moderators