mortalc Posted April 29, 2010 Share Posted April 29, 2010 How do I get Javascript to randomly allocate a value to a variable (say x)? And how do I get it to randomly allocate a value chosen from a defined set of numbers (say 1,3,5 or <=2)? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2010 Share Posted April 29, 2010 You can use Math.random to generate a random number between 0 and 1. If you want to pick a random item in a set, you can put your items in an array and then use Math.random to generate a random array index to use to get the item. There's an article here about making sure that the numbers you get really are random in the range you want to use:http://www.shawnolson.net/a/789/make-javas...dom-useful.html Link to comment Share on other sites More sharing options...
mortalc Posted April 29, 2010 Author Share Posted April 29, 2010 Could I set the variable with var x=Math.Random?I'll read the article, but in essence? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2010 Share Posted April 29, 2010 It's case-sensitive, this will set x to be a number between 0 and 1:var x = Math.random(); Link to comment Share on other sites More sharing options...
mortalc Posted April 29, 2010 Author Share Posted April 29, 2010 Ok, 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.