How to generate a random, unique, alphanumeric string?

PHP 7 standard library provides the random_bytes($length) function that generate cryptographically secure pseudo-random bytes. Example: $bytes = random_bytes(20); var_dump(bin2hex($bytes)); The above example will output something similar to: string(40) “5fe69c95ed70a9869d9f9af7d8400a6673bb9ce9” More info: http://php.net/manual/en/function.random-bytes.php PHP 5 (outdated) I was just looking into how to solve this same problem, but I also want my function to create a … Read more

Seeding the random number generator in Javascript

No, it is not possible to seed Math.random(). The ECMAScript specification is intentionally vague on the subject, providing no means for seeding nor require that browsers even use the same algorithm. So such a function must be externally provided, which thankfully isn’t too difficult. I’ve implemented a number of good, short and fast Pseudorandom number … Read more

MySQL select 10 random rows from 600K rows fast

A great post handling several cases, from simple, to gaps, to non-uniform with gaps. http://jan.kneschke.de/projects/mysql/order-by-rand/ For most general case, here is how you do it: SELECT name FROM random AS r1 JOIN (SELECT CEIL(RAND() * (SELECT MAX(id) FROM random)) AS id) AS r2 WHERE r1.id >= r2.id ORDER BY r1.id ASC LIMIT 1 This supposes … Read more

Getting random numbers in Java [duplicate]

The first solution is to use the java.util.Random class: import java.util.Random; Random rand = new Random(); // Obtain a number between [0 – 49]. int n = rand.nextInt(50); // Add 1 to the result to get a number from the required range // (i.e., [1 – 50]). n += 1; Another solution is using Math.random(): … Read more

Random color generator

Use getRandomColor() in place of “#0000FF”: function getRandomColor() { var letters=”0123456789ABCDEF”; var color=”#”; for (var i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } return color; } function setRandomColor() { $(“#colorpad”).css(“background-color”, getRandomColor()); } <script src=”https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script> <div id=”colorpad” style=”width:300px;height:300px;background-color:#000″> </div> <button onclick=”setRandomColor()”>Random Color</button>

How to request a random row in SQL?

See this post: SQL to Select a random row from a database table. It goes through methods for doing this in MySQL, PostgreSQL, Microsoft SQL Server, IBM DB2 and Oracle (the following is copied from that link): Select a random row with MySQL: SELECT column FROM table ORDER BY RAND() LIMIT 1 Select a random … Read more

How do I pick randomly from an array?

Just use Array#sample: [:foo, :bar].sample # => :foo, or :bar 🙂 It is available in Ruby 1.9.1+. To be also able to use it with an earlier version of Ruby, you could require “backports/1.9.1/array/sample”. Note that in Ruby 1.8.7 it exists under the unfortunate name choice; it was renamed in later version so you shouldn’t … Read more

How to generate a random int in C?

Note: Don’t use rand() for security. If you need a cryptographically secure number, see this answer instead. #include <time.h> #include <stdlib.h> srand(time(NULL)); // Initialization, should only be called once. int r = rand(); // Returns a pseudo-random integer between 0 and RAND_MAX. On Linux, you might prefer to use random and srandom.