Jump to content

Randomization


mortalc

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...