What’s the shortest way (within reason) to generate a random alpha-numeric (uppercase, lowercase, and numbers) string in JavaScript to use as a probably-unique identifier? Answer If you only want to allow specific characters, you could also do it like this: Here’s a jsfiddle to demonstrate: http://jsfiddle.net/wSQBx/ Another way to do it could be to use a special string that tells
Tag: random
Create an array with random values
How can I create an array with 40 elements, with random values from 0 to 39 ? Like I tried using solutions from here: http://freewebdesigntutorials.com/javaScriptTutorials/jsArrayObject/randomizeArrayElements.htm but the array I get is very little randomized. It generates a lot of blocks of successive numbers… Answer Here’s a solution that shuffles a list of unique numbers (no repeats, ever). If you want
Generate unique random numbers between 1 and 100
How can I generate some unique random numbers between 1 and 100 using JavaScript? Answer For example: To generate 8 unique random numbers and store them to an array, you can simply do this:
Write a truly inclusive random method for javascript
Javascript’s MATH object has a random method that returns from the set [0,1) 0 inclusive, 1 exclusive. Is there a way to return a truly random method that includes 1. e.g. While this always returns a number from the set [0,1] it is not truly random. Answer To put it bluntly, what you’re trying to do doesn’t make sense. Remember
Generate random string/characters in JavaScript
I want a 5 character string composed of characters picked randomly from the set [a-zA-Z0-9]. What’s the best way to do this with JavaScript? Answer I think this will work for you:
Is it correct to use JavaScript Array.sort() method for shuffling?
I was helping somebody out with his JavaScript code and my eyes were caught by a section that looked like that: My first though was: hey, this can’t possibly work! But then I did some experimenting and found that it indeed at least seems to provide nicely randomized results. Then I did some web search and almost at the top
Seeding the random number generator in Javascript
Is it possible to seed the random number generator (Math.random) in JavaScript? Answer No, it is not possible to seed Math.random(), but it’s fairly easy to write your own generator, or better yet, use an existing one. Check out: this related question. Also, see David Bau’s blog for more information on seeding.