developer

MySQL select 10 random rows from 600K rows fast

Question: ❓❓❓ How can I best write a query that selects 10 rows randomly from a total of 600k? Good Answer: 👌👌👌 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 Read more…

By admin, ago
developer

How to request a random row in SQL?

Question: ❓❓❓ How can I request a random row (or as close to truly random as is possible) in pure SQL? Good Answer: 👌👌👌 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, Read more…

By admin, ago
developer

How to generate a random number in C?

Question: ❓❓❓ Is there a function to generate a random number in C? Or will I have to use a third party library? Good Answer: 👌👌👌 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)); // should Read more…

By admin, ago
developer

Shuffling a list of objects

Question: ❓❓❓ I have a list of objects in Python and I want to shuffle them. I thought I could use the random.shuffle method, but this seems to fail when the list is of objects. Is there a method for shuffling object or another way around this? import random class Read more…

By admin, ago
developer

How do I pick randomly from an array?

Question: ❓❓❓ I want to know if there is a much cleaner way of doing this. Basically, I want to pick a random element from an array of variable length. Normally, I would do it like this: myArray = ["stuff", "widget", "ruby", "goodies", "java", "emerald", "etc" ] item = myArray[rand(myarray.length)] Read more…

By admin, ago
developer

Get random item from JavaScript array [duplicate]

Question: ❓❓❓ This question already has an answer here: Getting a random value from a JavaScript array 14 answers var items = Array(523,3452,334,31,...5346); How do I get random item from items? Good Answer: 👌👌👌 var item = items[Math.floor(Math.random()*items.length)]; Read More: 👉 https://stackoverflow.com/questions/5915096/get-random-item-from-javascript-array

By admin, ago
developer

PHP random string generator

Question: ❓❓❓ I’m trying to create a randomized string in PHP, and I get absolutely no output with this: <?php function RandomString() { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randstring = ''; for ($i = 0; $i < 10; $i++) { $randstring = $characters[rand(0, strlen($characters))]; } return $randstring; } RandomString(); echo $randstring; What Read more…

By admin, ago