\$\begingroup\$Can you define "random"? Do you mean a uniform distribution between 0 and 255 number of occurrences, any probability distribution where each number of occurences between 0 and 255 has a non-zero probability, or just an unpredictable amount of smileys?\$\endgroup\$
– BakuriuMar 6 '14 at 10:27

16

\$\begingroup\$My php solution is 0 bytes. It is an empty php file. Since you didn't specify what "random" means: For my custom made random number generator, I chose the distribution which gives zero with probability 1 and all other numbers with 0 probability (the degenerate distribution). I am sure you will find it obeys all of your rules.\$\endgroup\$
– Tim SeguineMar 6 '14 at 14:10

7

\$\begingroup\$I agree with @TimSeguine, if I could I would downvote. This is super easy, basically no option to be creative in any way, strange score system (mix of code-golf and popularity), the challenge itself isn't very innovativ.\$\endgroup\$
– print x div 0Mar 6 '14 at 15:09

\$\begingroup\$@DocMax: If we want a uniform distribution from 0 to 255 inclusive, yes. If not, it might as well be 9. :)\$\endgroup\$
– Ilmari KaronenMar 6 '14 at 9:05

7

\$\begingroup\$Why in the world does this answer have so many upvotes? There are other more interesting answers out there.\$\endgroup\$
– JustinMar 6 '14 at 21:05

4

\$\begingroup\$@Quincunx to point out how uninteresting this problem is. A good code-golf question should not be solvable by boring answers. If it is, it's a bad question.\$\endgroup\$
– o0'.Mar 9 '14 at 0:32

\$\begingroup\$@rafaelcastrocouto The question does not mention about how good the random generation must be. Using the timestamp seems to be a good compromise between number distribution and randomness.\$\endgroup\$
– FlorentMar 6 '14 at 17:44

\$\begingroup\$@rafaelcastrocouto It takes a random seed (The time at which you choose to run it), it's predictable (like any RNG) because you know the seed, still random.\$\endgroup\$
– George ReithMar 7 '14 at 0:01

1

\$\begingroup\$@Steve most golfing languages are never used in production, at least ES6 will actually be used.\$\endgroup\$
– howderekMar 7 '14 at 20:08

C - 116 (including artistic whitespace)

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 thought it would be because of the small number of characters to work with and the density of the code. Gets the point across at least.

Boring version:

x,i;main(){srand(time(0));for(x=rand()%256;i<x;i++)printf(":)");}

Note - Auto code counts are going to be off because I had to replace 8 tabs with spaces to get it to look the same in the code window. You can verify my count here if you want.

\$\begingroup\$This can be done with just 60 chars: x;main(){srand(time(0));for(x=rand()%256;--x;)printf(":)");}.\$\endgroup\$
– still_learningMar 8 '14 at 11:15

\$\begingroup\$@still_learning - Yeah, I know it can be quite a bit shorter, I structured the loop that way to keep the long strings I could divide distributed right.\$\endgroup\$
– CominternMar 9 '14 at 1:24

\$\begingroup\$Add 5 more bytes and you have my vote :)\$\endgroup\$
– JustinMar 6 '14 at 18:54

1

\$\begingroup\$Did it. Added a chinese smiley: ^__^ . Now because your comment has been upvoted once, there should be two more people voting for my answer ^__^\$\endgroup\$
– Mikaël MayerMar 7 '14 at 15:11

\$\begingroup\$id(9) is not random and id(9)%255 will never give 255.\$\endgroup\$
– grcMar 6 '14 at 5:00

2

\$\begingroup\$@grc: id(9) is random across multiple sessions but not across the same session. To get a random number across Please refer my output :-). And I agree, it should be id(9)%256\$\endgroup\$
– AbhijitMar 6 '14 at 5:44

\$\begingroup\$It appears to me that the output of id is always divisible by 8, so only 32 unique numbers can be produced from id(x)%256. If you divide by 8 first it should fix this.\$\endgroup\$
– grcMar 6 '14 at 6:00

\$\begingroup\$@grc: Ahh yes, I forgot about that, yet thought that implementation dependent, yet in cPython which is widely used, its how its implemented\$\endgroup\$
– AbhijitMar 6 '14 at 6:08

1

\$\begingroup\$@Abhijit: You can shave off one more character by swapping the multiplication around, since then you can get rid of the parenthesis (but have to add the space after print): print id(9)/8%256*':)' :)\$\endgroup\$
– Aleksi TorhamoMar 10 '14 at 0:59

\$\begingroup\$The question is a code-golf, so please include the character count in your answer. Also, try to "golf" your answer by removing unnecessary whitespace for example. And I also think that this answer can be a lot shorter, so for example it is not really necessary to create a class for each part of the smiley. It might be a good programming practice, but this is code-golf, code length is a lot more important than best practices.\$\endgroup\$
– ProgramFOXMar 7 '14 at 18:42

\$\begingroup\$All Get-<Something> functions in PowerShell are automatically aliased as <Something>, so you can save 4 bytes by getting rid of Get-\$\endgroup\$
– RynantMay 12 '14 at 18:36

1

\$\begingroup\$I really want to make this shorter, but it's so simple I'm afraid that's not possible. Fair warning about omitting "Get-": It's fine for short scripts like this, but it actually bloats the run time quite significantly. Try Measure-Command {Get-Random}; Measure-Command {Random} to see the difference. On my system, they're 0.1579 ms vs. 30.0963 ms. It's hardly noticeable when you're just running it once, but it can be a big deal when your script uses the command a lot more (say, 30,000 times)!\$\endgroup\$
– IsziMay 27 '14 at 15:26

\$\begingroup\$Thanks for the comment on aliasing. It's always useful to know the potential impact of something like aliasing, even when it is small.\$\endgroup\$
– DocMaxMay 27 '14 at 16:45

\$\begingroup\$I get a segmentation fault about half the time. I suppose it is intentional that you are trying to get a "random" variable by not initializing c?\$\endgroup\$
– skibrianskiMar 6 '14 at 4:47

\$\begingroup\$@skibrianski: Yes, there was some issue with the code. Brackets were misplaced and we need to work with absolute value\$\endgroup\$
– AbhijitMar 6 '14 at 5:53

\$\begingroup\$Have you tried (c&0xff) instead of abs(c)%256? This should work due to integer promotion.\$\endgroup\$
– n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳Mar 6 '14 at 6:48

\$\begingroup\$c&&main(printf(..),..); would be shorter.\$\endgroup\$
– ugorenMar 6 '14 at 6:51

\$\begingroup\$I think you missed urogen's other suggestion, to move the printf to main's arguments.\$\endgroup\$
– hvdMar 6 '14 at 13:24

Befunge 98 - 20 bytes (with unicode)

?1+:'ÿ`!j@"):",,>
@

? sends the IP in a random direction, either left, right, up, or down. After doing so, I increment a counter and compare it to 255: 1+:'ÿ`!. j@ will exit if this is the 256th time. "):" is the frowny face that I'm supposed to print, and ,, prints it (output: :)). The > is to ensure that if the IP goes left, it will change directions and go right.

On each iteration, there is a 2/3 probability that the loop will exit. This means that the probability that comparing to 255 is needed (assuming perfect random) is

Is that close enough to zero to justify not comparing to 255 (it would save me 10 bytes, bringing me down to 10 bytes)? For comparison, this number is about 2.158 × 10-122. The number of atoms in the universe is about 1080. The Planck length, the smallest measurable length (as in the universe does not allow for smaller), is 1.616199(97) × 10-35 m. This means that if this number were the size of the Planck length, the Planck length would be about

J - 18 char

Surprisingly, there is a subtle behaviour in the J interpreter that prevents a quick 12 character solution: the interpreter's REPL output is truncated after 256 characters, so you have to print directly to stdout to "do it right".

\$\begingroup\$The 12 char version doesn't print the string properly whenever the random number is 129 or greater, due to the nature of the J interpreter. Unless the rules change, only the 18 char solution is correct.\$\endgroup\$
– algorithmsharkMar 6 '14 at 3:57

\$\begingroup\$Assuming the z locale, you can get 17 with echo(+:?128)$':)'\$\endgroup\$
– AlliedEnvyMar 7 '14 at 1:04

\$\begingroup\$@AlliedEnvy You could get even shorter with echo':)'$~+:?256 (16 char), but it falls prey to the same bug as above, because echo uses 1!:2&2 and not &4, and thus respects the settings in 9!:36.\$\endgroup\$
– algorithmsharkMar 7 '14 at 1:33

\$\begingroup\$Actually, I was wrong to begin with. print':)'*(whatever) includes newlines. I didn't test it until right now. I learned something new today!\$\endgroup\$
– undergroundmonorailMar 6 '14 at 3:00

3

\$\begingroup\$I think you can save up to 7 characters by using os.urandom instead.\$\endgroup\$
– grcMar 6 '14 at 4:51

APL: 12 characters

(?255)⍴⊂':)'

In this code, ?255 generates a random number between 1 and 255. The ⍴ function repeats the argument to the right this number of times. The expression ⊂':)' encpsulates the string :) into a single object, since otherwise the expression 3⍴':)' would yield :): as opposed to :) :) :).

Edit: The behaviour of this function depends on the value of ⎕IO. If it's set to 0 (the default in many versions of APL) then ?255 will return a value in the range 0-254. If it is 1, the return value with be in the range 1-255.

\$\begingroup\$You don't need to convert i to an int immediately. Using a condition like ~~i-- or --i>0 should save 2 chars. You can also save a char with Math.random(t='').\$\endgroup\$
– grcMar 6 '14 at 4:39

1

\$\begingroup\$Intead of ~~() save 2 with 0|Math.... And another if you move t='' inside of the random() call\$\endgroup\$
– DocMaxMar 6 '14 at 4:45

\$\begingroup\$Just as a curiosity, this is the Harmony version, currently working only in Firefox: alert(':)'.repeat(Math.random()*256)).\$\endgroup\$
– manatworkMar 6 '14 at 7:49

\$\begingroup\$@manatwork Didn't knew that the repeat method exists (experimental for now, but exists). Anyway, I think that you may post an entirely new answer by yourself for this. I will upvote it.\$\endgroup\$
– Victor StafusaMar 6 '14 at 8:09

(PHP/Python/Perl/Javascript/others)-Polyglot - 0 bytes

Output 1:

Output 2:

Output 3:

You never specified a distribution for the random variable denoting the number of times, so this outputs from the degenerate one with the entire mass on zero. This is fine under the definition of range where it is equal to the codomain of a function, rather than its image.

Alright, it's a huge cheat, but since the id(9) code was successful despite not being guaranteed uniform, I figured this isn't entirely unclever.

\$\begingroup\$Well, you can lead a horse to water, but you can't make him learn measure theory.\$\endgroup\$
– Yonatan NMar 6 '14 at 5:08

4

\$\begingroup\$I'd argue that there's a difference between finding a loophole in the rules and intentionally syntactically misparsing them, but it's not my job to not submit troll answers when the question is practically begging for them. Downvotes, ahoy!\$\endgroup\$
– Yonatan NMar 6 '14 at 6:20

3

\$\begingroup\$+1 This is a perfectly reasonable interpretation of the question. The OP didn't specify the distribution. The degenerate distribution is a perfectly valid choice, no more incorrect than a uniform distribution. It may be a tired gag, but I am tied of "underspecified challenges". Make a challenge that is worth solving and nobody will post joke answers.\$\endgroup\$
– Tim SeguineMar 6 '14 at 14:18

3

\$\begingroup\$@JonathanVanMatre This isn't interpreting the challenge too literally. It is not our fault if the OP doesn't know what random means.\$\endgroup\$
– Tim SeguineMar 6 '14 at 14:23

3

\$\begingroup\$@JonathanVanMatre I have posted 3 challenges since joining this site. At the very least that is three more than you have. ;) And no I think you are wrong: I think it is clear from the comments that the OP in fact doesn't know what he wants. The link he provided implies that he wants a uniform distribution, but he hasn't complained, for example, that the Befunge answer doesn't use a uniform distribution. I stand by my statement that good questions don't attract troll answers.\$\endgroup\$
– Tim SeguineMar 6 '14 at 19:00

\$\begingroup\$That's an expression, not a program.\$\endgroup\$
– RhymoidMar 8 '14 at 23:15

\$\begingroup\$Your line is actually 49 characters, not 50. And I can shave it down to 44 for you: concat.flip replicate":)"<$>randomRIO(0,255) =D\$\endgroup\$
– KChalouxMar 10 '14 at 12:57

\$\begingroup\$KChaloux, that's awesome! I understand the concat.flip but I did not know <$>, thanks! And Rhymoid, you are right, to make it into a compilable program we'd have to add main= to it's beginning, which costs an extra 5.\$\endgroup\$
– linseMar 10 '14 at 21:59

Code Golf Stack Exchange is a site for recreational programming competitions, not general programming questions. Challenges must have an objective scoring criterion, and it is highly recommended to first post proposed challenges in the Sandbox.