You have to create a code which prints
:)
a random number of times from 1 to 255 inclusive. Your output may not contain any whitespace.
Your score :
The count in bytes
EDIT : Also show 3 outputs of your code
Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It only takes a minute to sign up.
Sign up to join this communityYou have to create a code which prints
:)
a random number of times from 1 to 255 inclusive. Your output may not contain any whitespace.
Your score :
The count in bytes
EDIT : Also show 3 outputs of your code
say ":)"x 256.rand
256.rand gives random number that is equal or higher than 0 or lower than 256. x multiplies the string given number of times, and floors the integer.
Sample:
glitchmr@feather ~> perl6 -e 'say ":)"x 256.rand'
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
glitchmr@feather ~> perl6 -e 'say ":)"x 256.rand'
:):):):):):):):):):):):)
say ':)' x (^256).pick better, but there is no way to make it shorter than using rand. Although it does come in handy if you want to output all of the possible outputs in random order .say for ":)" Xx (^256).pick(*)
\$\endgroup\$
Nov 27, 2014 at 15:15
This is how we do it without using implementationally experimental repeat commands...
console.log(Array(Date.now()%257).join(":)"))
Output 1:
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
Output 2:
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
Output 3:
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
One thing I have found that both a random throw of 0 or 1 will yield no characters, so the 0-255 smileys will still hold!
repeat is not experimental. It was validated by ES6 committee. Only its implementation is experimental.
\$\endgroup\$
repeat...
\$\endgroup\$
Mar 6, 2014 at 11:44
Array.join trick. If you type [1, 2, 3].join("-") you will get "1-2-3". Note two instances of dash, not three.
\$\endgroup\$
Mar 7, 2014 at 16:37
alert(Array(0|Math.random()*257).join(':)'))
Note: zero-length string twice as common as other lengths (2-510).
Edit: 5 fewer characters using Date.now()%257 instead:
alert(Array(Date.now()%257).join(':)'))
*O256":)
I'm surprised Pyth hasn't been used yet.
Output 1
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
Output 2
:):)
Output 3
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
* Multiply...
O256 Random from range(255)
":) ...By ":)". End quote is inferred.
(Printing is implicit)
<?=str_pad('',rand(0,255),':)');
Or if we do not need to include php open tag, then it is 30:
=str_pad('',rand(0,255),':)');
Whichever suits you best
= (no space in front). If you add php opening tag <? before any of them, they will both work?
\$\endgroup\$
Mar 6, 2014 at 14:54
= is not valid syntax. <?echo 1; is valid syntax, btw(there need not be a space before echo). You see the point I am making but want to language lawyer anyway.
\$\endgroup\$
Mar 6, 2014 at 15:01
<? if you put them in php interpreter. But I see your point, that's what I thought too, that's why I wrote 32 as length.
\$\endgroup\$
Mar 6, 2014 at 17:16
Extracting randomness from the pointer provided. Granted, it is aligned (multiples of 8) but it's a start:
main(a,b){while(b--&255)printf(":)");}
Fixed at expense of 5 chars:
main(a,b){b/=8;while(b--&255)printf(":)");}
Edit: noticed that newlines are not allowed. A solution is to replace puts with printf adding two chars. I now noticed that Abhijit's solution has the same idea. Sorry.
Excel Speadsheet Formula (also works in Google docs) - 21
=REPT(":)",RAND()*250)
Output 1:
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
Output 2:
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
Output 3:
:):):):):):):):):):):):):):):):):):)
System.out.println(new String(new char[(int)(Math.random()*255)]).replace("\0",":)"));
Fairly long, but interesting.
This bf program (49 bytes) reads a byte from stdin (0-255) and prints that many smileys:
+[--------->+<]>+<++[------>>+<<]>>--<<,[->.>.<<]
Compile this to a file a and execute
./a</dev/random
Alternatively, assuming your PATH is set up properly (13 bytes):
a</dev/random
62 bytes in total, guaranteed randomness.
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
Not the greatest attempt in the world (complete with crappy LCG PRNG), but I had to do something. This is a COM file. The raw source is (in hex):
B42CCD210FAFCA32EDBA1601B409E304CD21E2FCCD203A2924
In more readable ASM:
; Get system time (for random amount)
mov ah, 02Ch
int 021h
; Make cl pseudo-random
imul cx, dx
xor ch, ch
; Prepare for our print
mov dx, Smile
mov ah, 9
; Loop a random number of times
jcxz Done
MainLoop:
int 021h
loop MainLoop
Done:
; End
int 020h
; Our smile
Smile:
db ':)$'
Sample output:
C:\TEMP>smiley1
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
C:\TEMP>smiley1
:):):):):):)
C:\TEMP>smiley1
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):)
EDIT Changed to int 020h per DocMax
int 20 instead of xor ah,ah/int 21 to save 2 more bytes.
\$\endgroup\$
JavaScript
Can't you just do something like this?
var s = ""; for(var a = 0; a<Math.random()*255;a++){s+=":)";} alert(s);
Python 3, 38 bytes.
from random import*
':)'*randrange(256)
OUTPUTS
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
print(':)'*__import__('random').randrange(256)).
\$\endgroup\$
Mar 8, 2014 at 20:42
JAVA 61
for(int i=0;i<Math.random()*255;i++){System.out.print(":)");}
Output:
:):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):)
class C{public static void main(String[]a){...}} to your code for it to be compilable.
\$\endgroup\$
Mar 6, 2014 at 22:27
import random;':)'*random.randint(0,255)
output:
>>> import random;':)'*random.randint(0,255)
':):):)'
>>> import random;':)'*random.randint(0,255)
':):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)'
>>> import random;':)'*random.randint(0,255)
':):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)'
PHP - 28
=str_pad('',rand()&255,':)')
or
=str_repeat(':)',rand()&255)
These are 30 if you include
<?
; (unless you have {} block, or something like this), and = is not a token by itself (-r that allows you not to specify <? considers program that begins with = to be a syntax error). Change your program to have <? at beginning, and ; at end.
\$\endgroup\$
Mar 7, 2014 at 16:40
Windows Powershell - 30 21
Thanks to manatwork for telling me I don't need "-maximum" to specify upper limit
":)"*(get-random 255)
:):):):):):):):):):):):):):):):):)
":)"*(get-random 255)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
":)"*(get-random 255)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
-Maximum. At least in PowerShell 2.0.
\$\endgroup\$
Mar 6, 2014 at 15:27
Get-Random call. (Like in my own answer).
\$\endgroup\$
random 255 instead get-random 255
\$\endgroup\$
Python 116
a=list(string.printable*255)
random.shuffle(a)
print ''.join([ x+y for x,y in zip(*[iter(a)]*2) if x==":" and y==")"])
Relying on that popularity aspect to bring score down...
(61 characters plus newline)
main(){int x=time((int*)0)&510;while(x--)putchar(x&1?58:41);}
time; however, pointers are all represented the same way on many platforms: the function call time((long*)0) won't generate code that is any different from time((int*)0).putchar call: the IBM mainframe salesman was out playing golf.This version elicits a "data definition has no type or storage class" diagnostic from GCC:
x;main(){x=time((int*)0)&510;while(x--)putchar(x&1?58:41);}
According to ISO C syntax, a declaration must have specifiers, and according to the Constraints, it "shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration." (ISO 9899:1999 6.7)
(int *) stays; without it, we are passing an int to time.
\$\endgroup\$
int and pointers, which have a different size on a whole lot of mass-produced, consumer-grade hardware! Worrying about non-ASCII systems is what is "lolportable".
\$\endgroup\$
print((':)'):rep(math.random(255)))
Sample output:
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
(the last time I tried to post this, the question had been suddenly closed)
This fork bomb is sad because it can never explode.
:(){ :|\)& };:
bash: ): command not found
bash: ): command not found
bash: ): command not found
If you execute this, you will probably want to pkill -1 bash, since each process survives for only moments.
To make it stop automatically, it can be changed to:
r=$[RANDOM>>7];:(){ [ $[r--] = 0 ]||:|\)& };:
Array(Math.floor(Math.random()*255)).join(':)')
output #1
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
output #2
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
output #3
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
~~(Math.random()*255) instead of Math.floor(Math.random()*255). Also get rid of your spaces.
\$\endgroup\$
import random;random.randrange(0,256)*':)'
could have been reduced to 28 chars if import wasn't counted.
output:
:):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
from ... import*: from random import*;randrange(0,256)*':)'
\$\endgroup\$
yes :\)|head -$[RANDOM>>7]|tr -d \\n
I borrowed the creative use of RANDOM from @DigitalTrauma.
Testing:
$ yes :\)|head -$[RANDOM>>7]|tr -d \\n
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
$ yes :\)|head -$[RANDOM>>7]|tr -d \\n
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
$ yes :\)|head -$[RANDOM>>7]|tr -d \\n
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
":)"*util.Random.nextInt(256)
res9: String = :):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):
):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):
):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):
):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):
):):):):):):):):):):):):)
res10: String = :):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):
):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):
):):):):):):):):):):):):):):):):):):)
res11: String =
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):
):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):
):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):
):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):)
I love scala =)
str_repeat(':)',rand(0,255))
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
(doseq [t (take (rand-int 256) (repeat ":)"))]
(print t))
import random
x=random.randint(0,255)
print(':)'*x)
:) between 0 and 255 times. This program cannot print :) 42 times, for example. Also, it could be shortened, by removing whitespace around = operator. If you use Python 2, you can remove parenthesis around print. You should also specify your language.
\$\endgroup\$
Mar 9, 2014 at 8:22
using System;using System.Linq;namespace G{class Program{static void Main(){Console.WriteLine(string.Join("",Enumerable.Repeat(":)",new Random().Next(255))));}}}
Pretty Print:
using System;
using System.Linq;
namespace G
{
class Program
{
static void Main()
{
Console.WriteLine(string.Join("",Enumerable.Repeat(":)", new Random().Next(255))));
}
}
}
Output:
1
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):) :):):):):):):):):):):):):):):):):):):):):):):):):):):) Press any key to continue . . .
2
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):) :):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):) :):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):) :):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):) :):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):) :):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):) :):):):):) Press any key to continue . . .
3
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):) :):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):) :):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):) :):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):) Press any key to continue . . .
using System;class P{static void Main(){Console.WriteLine(new string('.',new Random().Next(256)).Replace(".",":)"));}}. Yours can be shortened down to 142 by removing the namespace and renaming Program to P.
\$\endgroup\$
(counting the newline as one character)
#include <iostream>
main(){for(int k;++k<(((long)&k)%255);)std::cout<<":)";}
Actually, using a while and some C-style output I managed to shave off a few more:
#include <cstdio>
main(){int k;while(++k<(((long)&k)%255))printf(":)");}
Output examples:
:):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
:):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)
Python 3.x 47
import random;print(':)'*random.randint(0,255))
as Doorknob said, boring.