171
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 random ...
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)];
111
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 ...
89
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 coordinates. ...
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 ...
74
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 ...
62
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]);
}
}
}
57
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.
answered Dec 3 '16 at 18:29
Stewie Griffin
44.5k1212 gold badges122122 silver badges283283 bronze badges
54
JavaScript (ES6), 25
":)".repeat(new Date%256)
Outputs (REPL):
> ":)".repeat(new Date%256)
":):):):):)"
> ":)".repeat(new Date%256)
":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):...
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;
41
R, 1 byte
t
Outputs the function's source code and a memory pointer address which changes with every (re-)start of R.
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 ...
38
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 ...
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")
37
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 ...
37
Wolfram Language (Mathematica), 20 bytes
RandomPoint@Sphere[]
Try it online!
Does exactly what it says on the tin.
36
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". ...
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",...
31
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 ...
31
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 ...
answered Nov 30 '16 at 20:41
Martin Ender
189k6262 gold badges418418 silver badges928928 bronze badges
31
Minecraft, 5 4 bytes
op 8
Used by typing into a server's console or a command block and giving it power. Can be run from the chat interface by prepending a /.
Usually this does nothing, but if there's a player with the username "8" on the server, they will be given operator permissions. Note that while Minecraft normally requires usernames to be 3 ...
30
GolfScript, 12 characters
":)"256rand*
Nothing really interesting here.
30
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 ;O&...
30
Shell
Reads a single sample from the microphone stream and prints its least-significant bit, which should be dominated by noise.
EDIT: Changed to unmute the microphone ... and everything else too!
# Warning - unmutes EVERYTHING!
for DEV in `amixer | grep Simple | sed -e "s/.*'\(.*\)'.*/\\1/" -e "s/ /~/g"`
do
amixer -q -- sset "`...
29
Mathematica (18)
Let me a little cheat
= 15char ASCII pwd
&(^F7yP8k:*1P<t
P.S. not safety :)
Only top voted, non community-wiki answers of a minimum length are eligible
Related Tags
random × 292code-golf × 268
string × 54
ascii-art × 37
number × 20
game × 19
array-manipulation × 14
generation × 11
math × 10
permutations × 10
code-challenge × 9
graphical-output × 9
probability-theory × 9
combinatorics × 8
sequence × 7
kolmogorov-complexity × 7
grid × 7
animation × 7
popularity-contest × 6
geometry × 6
integer × 5
arithmetic × 4
sorting × 4
internet × 4
cryptography × 4