Tura Posted December 30, 2005 Share Posted December 30, 2005 I couldn't find how to make over two random statements. I tried codes like this: <script type="text/javascript">var r=Math.random()if (r>0.5) {document.write("(one)")}if(r=1){document.write("(two)")}else{document.write("(three)")}</script> But that didn't work. Is there a way to make over two random statements so that they all appear with about the same % chance? Link to comment Share on other sites More sharing options...
Jonas Posted December 30, 2005 Share Posted December 30, 2005 I suppose you could try the switch statement.http://www.w3schools.com/js/js_switch.asp Link to comment Share on other sites More sharing options...
lugos Posted December 30, 2005 Share Posted December 30, 2005 if(r=1){document.write("(two)")} You are assigning 1 to r, not comparing; that will always return true. '==' is the comparison operator. I don't know if that'll make a difference. Link to comment Share on other sites More sharing options...
Tura Posted December 30, 2005 Author Share Posted December 30, 2005 I tried this without success: <script type="text/javascript">switch(n){case 1: document.write("(1)") break case 2: document.write("(2)") breakdefault: document.write("(3)"}</script> Link to comment Share on other sites More sharing options...
hacknsack Posted December 31, 2005 Share Posted December 31, 2005 Tura,I'm not sure exactly how you want the outcome calculated.This example breaks the random number down to "thirds"( .33 ) function mkRandom(){ var r = Math.random(); switch (true){ case(r < .33): alert('First'); break; case(r < .66): alert('Middle'); break; default: alert('Last'); break; } } Let us know if you have any questions, Link to comment Share on other sites More sharing options...
Jonas Posted December 31, 2005 Share Posted December 31, 2005 Tura,I'm not sure exactly how you want the outcome calculated.This example breaks the random number down to "thirds"( .33 ) function mkRandom(){ var r = Math.random(); switch (true){ case(r < .33): alert('First'); break; case(r < .66): alert('Middle'); break; default: alert('Last'); break; } } Let us know if you have any questions, <{POST_SNAPBACK}> I don't think that switch statement will work, because you have to compare upwards and downwards. If the Math.random is .25, it will be true for both < .33 and <.66 Link to comment Share on other sites More sharing options...
Tura Posted January 1, 2006 Author Share Posted January 1, 2006 Yeah. That's simalar to another thing I tried. Both appeared.Hmm... Is there a way to do between numbers instead of less than or more than? Link to comment Share on other sites More sharing options...
hacknsack Posted January 1, 2006 Share Posted January 1, 2006 While I tested the original code in IE, FF and Opera, I agree with Jonas that it will be a better model if we adjust our tests. <html> <head> <title></title> </head> <body> <script type="text/javascript"> function mkRandom(){ var r = Math.random(); switch (true){ case(r <= .33): document.write('One'); break; case(r > .33 && r <= .66): document.write('Two'); break; default: document.write('Three'); break; } } mkRandom(); </script> </body></html> Thanks, Link to comment Share on other sites More sharing options...
Tura Posted January 4, 2006 Author Share Posted January 4, 2006 That works, thanks! Link to comment Share on other sites More sharing options...
hacknsack Posted January 4, 2006 Share Posted January 4, 2006 You are very welcome, :)Glad to hear you worked it out. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now