Tag: random

Question: ❓❓❓ I observed that rand() library function when it is called just once within a loop, it almost always produces positive numbers. for (i = 0; i < 100; i++) { printf("%d\n", rand()); } But when I add two rand() calls, the numbers generated now have more negative numbers. Read more…

Question: ❓❓❓ I realize the Swift book provided an implementation of a random number generator. Is the best practice to copy and paste this implementation in one’s own program? Or is there a library that does this that we can use now? Good Answer: 👌👌👌 Use the standard library functions Read more…

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…

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…

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…

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…

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…