Get a random item from a JavaScript array [duplicate]
var item = items[Math.floor(Math.random()*items.length)];
var item = items[Math.floor(Math.random()*items.length)];
Answer in one line: ”.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N)) or even shorter starting with Python 3.6 using random.choices(): ”.join(random.choices(string.ascii_uppercase + string.digits, k=N)) A cryptographically more secure version: see this post ”.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N)) In details, with a clean function for further reuse: >>> import string >>> import random >>> … Read more
Try random.randrange: from random import randrange print(randrange(10))
The de-facto unbiased shuffle algorithm is the Fisher-Yates (aka Knuth) Shuffle. You can see a great visualization here (and the original post linked to this) function shuffle(array) { let currentIndex = array.length, randomIndex; // While there remain elements to shuffle. while (currentIndex != 0) { // Pick a remaining element. randomIndex = Math.floor(Math.random() * currentIndex); … Read more
The other answers explain why, but here is how. Given an instance of Random: Random r = new Random(-229985452) The first 6 numbers that r.nextInt(27) generates are: 8 5 12 12 15 0 and the first 6 numbers that r.nextInt(27) generates given Random r = new Random(-147909649) are: 23 15 18 12 4 0 Then … Read more
Algorithm To generate a random string, concatenate characters drawn randomly from the set of acceptable symbols until the string reaches the desired length. Implementation Here’s some fairly simple and very flexible code for generating random identifiers. Read the information that follows for important application notes. public class RandomString { /** * Generate a random string. … Read more
Use random.choice(): import random foo = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] print(random.choice(foo)) For cryptographically secure random choices (e.g., for generating a passphrase from a wordlist), use secrets.choice(): import secrets foo = [‘battery’, ‘correct’, ‘horse’, ‘staple’] print(secrets.choice(foo)) secrets is new in Python 3.6. On older versions of Python you can use the random.SystemRandom class: import random … Read more
The Random class is used to create random numbers. (Pseudo-random that is of course.). Example: Random rnd = new Random(); int month = rnd.Next(1, 13); // creates a number between 1 and 12 int dice = rnd.Next(1, 7); // creates a number between 1 and 6 int card = rnd.Next(52); // creates a number between … Read more
function randomIntFromInterval(min, max) { // min and max included return Math.floor(Math.random() * (max – min + 1) + min) } const rndInt = randomIntFromInterval(1, 6) console.log(rndInt) What it does “extra” is it allows random intervals that do not start with 1. So you can get a random number from 10 to 15 for example. Flexibility.
There are some examples on the Mozilla Developer Network page: /** * Returns a random number between min (inclusive) and max (exclusive) */ function getRandomArbitrary(min, max) { return Math.random() * (max – min) + min; } /** * Returns a random integer between min (inclusive) and max (inclusive). * The value is no lower than … Read more