Hot answers tagged

172

C It's important to decide who is buying as quickly as possible, so as not to waste precious drinking time - hence C is the obvious choice in order to get maximum performance: #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { const char *buyer; int n; srand(time(NULL)); // make sure we get a good ...


164

PHP Couldn't let this go, so here is another one: $f = fopen('/dev/random','r'); $s = fread($f, 4); fclose($f); $names = ['John', 'Jeff', 'Emma', 'Steve', 'Julie']; echo $names[$s % count($names)];


110

WinDbg, 1 byte # Wow! Never expected a 1 byte solution from WinDbg! # searches for a disassembly pattern, but since there's no parameters, it looks to just return the next assembly instruction in whatever dump/process you're attached to. Not sure the logic for setting the initial address, but it does. Sample output: 0:000> # Search address set to ...


85

Haskell It's too transparent if it always returns the same name so try the following import Control.Monad import System.Exit import Control.Concurrent import Control.Concurrent.MVar data Person = John | Jeff | Emma | Steve | Julie deriving (Show, Enum) next Julie = John next p = succ p rotate :: MVar Person -> IO () rotate mp = modifyMVar_ mp (return ...


85

Minecraft 1.12 Redstone Command Blocks, 4,355 2,872 bytes (Size determined by saved structure block file size.) Here is a full YouTube overview, but I'll try to outline the code below. Setup Routine: This sets up the 40x40 grid of Minecraft armor stands. Armor stands are necessary because Minecraft has no way to substitute variables into world ...


75

Bash - maximum simplicity A very simple example - let's avoid any problems by doing it the textbook way. Don't forget to seed the generator from the system clock for a good result! #!/bin/bash names=(John Jeff Emma Steve Julie) # Create an array with the list of names RANDOM=$SECONDS # Seed the random generator with seconds since ...


60

Java 7, 33 30 27 bytes int a(){return hashCode();} Because Java.


57

C# using System; using System.Linq; namespace PCCG { class PCCG31836 { public static void Main() { var names = new string[]{ "John", "Jeff", "Emma", "Steve", "Julie" }; var rng = new Random(); names.OrderBy(name => rng.Next()); Console.WriteLine(names[0]); } } }


54

JavaScript (ES6), 25 ":)".repeat(new Date%256) Outputs (REPL): > ":)".repeat(new Date%256) ":):):):):)" > ":)".repeat(new Date%256) ":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):...


52

MATLAB, 3 bytes why why provides answers to almost any question. A few examples: why The programmer suggested it. why To fool the tall good and smart system manager. why You insisted on it. why How should I know? This is shorter than any rand function I can think of.


44

PowerShell $names = @{0='John'; 1='Jeff'; 2='Emma'; 3='Steve'; 4='Julie'} $id = random -maximum $names.Length $names[$id] This will always output John.


43

Operation Flashpoint scripting language, 48 bytes f={s="duck\n";s+([s,""]select random 1)+"goose"} Always prints either one or two ducks. random 1 returns a (floating point) number between 0 and 1. That number is passed as an argument to select along with the array [s,""]. The random number is then rounded to the nearest integer (either 0 or 1), and the ...


42

Q show rand `John`Jeff`Emma`Steve`Julie; exit 0;


40

Pyth, 8 u+G!OTsQ Try it online This uses Pyth's second mode on reduce, that looks for repeated input then exits. Explanation u+G!OTsQ ## Implicit: Q=eval(input()) u sQ ## reduce with 2 arguments, which causes a loop until the reduce gets the ## same argument twice +G ## lambda G,H: G + ... !OT ## boolean not of random ...


40

R, 1 byte t Outputs the function's source code and a memory pointer address which changes with every (re-)start of R.


38

World of Warcraft 81 Bytes Here's a macro that you can run in World of Warcraft. /run for x=1,random(1,9) do SendChatMessage("Duck") end; SendChatMessage("Goose")


36

Python 2, 55 bytes from random import* lambda a,b:a+b+18-len(`1+random()`) This is an absolutely bizarre way to do it. The function random gives a float in [0,1), and its string representation by default has 16 digits after the decimal point, for 18 characters total. But, because trailing 0's are omitted, it might be shorter. Reading digits from the end, ...


36

huh?, 0 bytes An empty program still produces output. The last lines of the Python interpreter that are executed: print "..." f = open('Notes.txt', 'w') f.write(time.strftime("%c") + " - The user tried to give me commands again. I still have no idea what they are talking about...\n") At the end of a program, the Python interpreter will print ..., then it ...


36

x86 Machine Code (for Intel Ivy Bridge and later), 17 bytes 31 C9 0F C7 F0 31 D2 F7 F6 42 01 D1 39 F2 74 F2 C3 The above bytes of code define a function that simulates an exploding die. It takes a single input, passed in the ESI register, indicating the maximum number of the die. It returns a single value in the ECX register, which is the result of the ...


35

Minecraft <1.13, 72 54 bytes Sorry, I had to. Instructions: Create a new Minecraft world in Creative Mode Find the save folder for that world, and place the following code in data/functions/minecraft/ddg.mcfunction Run /function ddg in the game console How it works: Outputs the word "duck" for every entity in the world, then outputs the word "goose". ...


35

Wolfram Language (Mathematica), 20 bytes RandomPoint@Sphere[] Try it online! Does exactly what it says on the tin.


34

Perl use strict; my @people = qw/John Jeff Emma Steve Julie/; my @index = int(rand() * 5); print "Person @index is buying: $people[@index]\n";


34

Operation Flashpoint scripting language, 643 624 bytes f={l=["""","!","#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\","]","^","_","`","a","b","c","d","e","f","g","h",...


33

C - 116 (including artistic whitespace) x,i; main (){ srand (time (0)); for (x= rand() %256; i<x; i++) printf(":)");} I never get to do ASCII art code, and the 65 needed characters in the code are already running long as is... This was actually harder than I ...


30

GolfScript, 12 characters ":)"256rand* Nothing really interesting here.


30

Labyrinth, 5 bytes v ! @ Either prints 0 or nothing (50% chance each). Try it online! There is a very specific case in which Labyrinth exhibits random behaviour: There must be a wall in front of the instruction pointer and behind it. There must be a non-wall left and right of the instruction pointer. The current top of the stack must be zero. If all of ...


29

k (18 17 16 chars) Back to the original approach, credit to CS for the improvement. (+/4-){3+18?3}/0 Other approach (17 chars), same method as the J solution, H/T to CS 4+a,-a:9?2 -18?18 Old version: (72-+/){18?3+!3}/0 Not susceptible to stack-overflow and runs in fixed amount of space.


29

Mathematica (18) Let me a little cheat = 15char ASCII pwd &(^F7yP8k:*1P<t P.S. not safety :)


29

Scala REPL 1337 Scala makes everyone happy. Need 266 votes to lower the score. (""" :^) :^( :^[ :^] :^D :^P :^b :^& :o) :o( :o[ :o] :oD :oP :ob :o& :O) :O( :O[ :O] :OD :OP :Ob :O& :) :( :[ :] :D :P :b :& ;^) ;^( ;^[ ;^] ;^D ;^P ;^b ;^& ;o) ;o( ;o[ ;o] ;oD ;oP ;ob ;o& ;O) ;O( ;O[ ;O] ;OD ;OP ;Ob ;...


Only top voted, non community-wiki answers of a minimum length are eligible