Darkness Posted July 6, 2007 Share Posted July 6, 2007 Hello,Does anyone know how to create a random variable? Such as, a random variable between 1 and 6.Thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 6, 2007 Share Posted July 6, 2007 Use Math.random. Link to comment Share on other sites More sharing options...
Darkness Posted July 6, 2007 Author Share Posted July 6, 2007 Hmm.. I don't get it. How would you put that?var Variable= "1,4.random";or something? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 6, 2007 Share Posted July 6, 2007 What? Javascript has an object called Math, and the Math object has a method called random that returns a pseudorandom number between 0 and 1. If you want to make it between 1 and 6, you multiply it by 5 and add 1. Link to comment Share on other sites More sharing options...
Darkness Posted July 6, 2007 Author Share Posted July 6, 2007 Ah, how would you set the variable, though?I tried var Variable= "(Math.floor(Math.random()*3)"; That won't work, however. I need the variable, Variable, to be a random number between one and three. Link to comment Share on other sites More sharing options...
aspnetguy Posted July 6, 2007 Share Posted July 6, 2007 something like this http://www.javascriptkit.com/javatutors/randomnum.shtml Link to comment Share on other sites More sharing options...
justsomeguy Posted July 6, 2007 Share Posted July 6, 2007 Ah, how would you set the variable, though?I tried var Variable= "(Math.floor(Math.random()*3)"; That won't work, however. I need the variable, Variable, to be a random number between one and three. No, that won't work. When you use quotes you are using a string. You are saying that the variable Variable contains the string "(Math.floor(Math.random()*3)" instead of the result of that code getting executed. Anytime you put anything in quotes, you are using a literal value, not an operation.var num = Math.floor((Math.random() * 2) + 1); Link to comment Share on other sites More sharing options...
Darkness Posted July 7, 2007 Author Share Posted July 7, 2007 Oh. That's how. Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.