r/badcode • • Nov 23 '20

[deleted by user]

[removed]

14 Upvotes

15 comments sorted by

14

u/Mabi19_ Nov 24 '20 edited Nov 24 '20

This isn't really a one-liner, but it is still tweetable!

echo -e $(curl -s "https://www.random.org/integers/?num=75&min=0&col=1&max=255&base=10&format=plain"|sort -g|uniq|head -n 50|sed 's/\([0-9]\+\)/\\033[48;2;\1;\1;\1m/g')'\033[49m'

I'd say it's fairly self-explanatory. Also, woo truly random numbers!

(Note: Tested using the GNU toolchain)

4

u/blackjezza Nov 24 '20

Have no clue what sed is doing there but it works well. Pretty cool!

5

u/Mabi19_ Nov 24 '20

The sed is replacing the numbers from random.org to ANSI escape sequences for setting background colors to an RGB value (which is just the same number for R, G and B)

10

u/dodecakiwi Nov 26 '20

Uses Windows.Forms to open 50 windows along the top of the screen, in order, each with a different background color.

    void gray_scale(int intervals)
    {
        Random random = new Random();
        var gray_start = random.Next(0, 256 - intervals);
        var gray_end = random.Next(gray_start + intervals, 256);
        var interval = (gray_end - gray_start) / (double)intervals;

        var screen_width = Screen.PrimaryScreen.Bounds.Width;
        var window_width = screen_width / intervals;

        for (int i = 0; i < intervals; i++)
        {
            Form form = new Form();
            var gray = (int)Math.Floor(gray_start + i * interval);
            form.BackColor = Color.FromArgb(255, gray, gray, gray);
            form.StartPosition = FormStartPosition.Manual;
            form.Location = new Point(i * window_width, 0);
            form.Width = window_width;
            form.Height = 100;
            form.Text = gray.ToString("X");
            form.ControlBox = false;
            form.Show();
        }
    }

5

u/RecDep Nov 27 '20 edited Nov 27 '20

One liner in Haskell.

import Graphics.Gloss
import Control.Arrow
import Control.Applicative (liftA2)
import Control.Monad
import Data.Bifunctor (bimap)
import System.Random 
import Data.Bits 
import Data.Tuple.Curry (uncurryN)
import Data.Function ((&))

main :: IO ()
main = display (InWindow "hi reddit" (150, 300) (join (,) 0)) ((join (join (join makeColorI)) 0o400)) . pictures . flip (zipWith color . (& (mkStdGen >>> randomRs (0, 256) >>> ((liftA2 id (uncurryN makeColorI . join (join (,,))) (abs . xor 0x25 . (`subtract` 0x1f) . xor 0xd)) <$>)))) (1 & ((`enumFromTo` 5) &&& (`enumFromTo` 10) >>> join bimap ((30 *) <$>) >>> uncurry (liftA2 (,)) >>> ((uncurry (flip flip (join rectangleSolid 0x1e) . translate)) <$>))) =<< (last <$> replicateM 2 randomIO)
→ More replies (2)

5

u/[deleted] Nov 25 '20

Yes, it is getting colors from an image on the internet. I also added the usual Python horror. It works most of the time... most.

try:
    from PIL import Image
    from urllib.request import urlopen
    #use a fully grayscale image for program to work
    img = Image.open(urlopen("https://i.pinimg.com/736x/86/39/fd/8639fd50a4f18ae041f636c1e51ce317--fifty-shades-of-grey--shades.jpg")).convert('RGB') #the forbidden sauce
    shades = []
    while True == True == True:
        n = 0
        from random import randint
        rgb = img.getpixel((randint(1, img.size[0] - 1), randint(1, img.size[1] - 1)))
        #check if christian grey is unique, cannot be too dark or too light
        while False == False:
            if n == len(shades): break
            elif(shades[n] == rgb[0] and shades[n] == rgb[1] and shades[n] == rgb[2]
            or rgb[0] + rgb[1] + rgb[2] < 69 or rgb[0] + rgb[1] + rgb[2] > 420 / (6 / 9)):
                rgb = img.getpixel((randint(1, img.size[0] - 1), randint(1, img.size[1] - 1)))
                n = 0
            n += 3
        shades.extend(rgb) #todo: use list.append instead
        if 50 == len(shades) / 3:
            n = 3
            from random import choice
            shades = sorted(shades, reverse=choice([False, True]))
            #print greys in hex (i like hex)
            while True != False:
                print("#" + str(hex(shades[n - 3]))[2] + str(hex(shades[n - 3]))[3] + str(hex(shades[n - 2]))[2] + str(hex(shades[n - 2]))[3] + str(hex(shades[n - 1]))[2] + str(hex(shades[n - 1]))[3])
                n += 3
                if n == len(shades) + 3: quit() #end program, it doesn't deserve to live
except IndexError:
    print("Whoops! Something went wrong!")

7

u/JanStreams Nov 26 '20

False == False is the new True

2

u/bobdarobber Dec 01 '20

while true is true is true

4

u/allbyoneguy Nov 25 '20 edited Nov 28 '20

Did my best, it COULD be very fast, but it's pretty luck based.
Generate random R, G and B, if they are all equal in the same loop it's a gray value, add to the list and try and find the next one. Discard doubles and print when the list is 50 items long

import random

rand1 = random.randrange(10, 220)
rand2 = random.randrange(10, 220)
rand3 = random.randrange(10, 220)
fiftylist = []
combinations = 0
converthex = ""
doubles = 0

converthex = "#" + '%02x%02x%02x' % (rand1, rand2, rand3)
def truerandom():
    global rand1
    global rand2
    global rand3
    global converthex
    rand1 = random.randrange(10, 220)
    rand2 = random.randrange(10, 220)
    rand3 = random.randrange(10, 220)
    converthex = "#" + '%02x%02x%02x' % (rand1, rand2, rand3)
    global combinations
    combinations += 1
    print("Tried " + str(combinations) + " combinations, got " + str(len(fiftylist)) + " so far" + " and discarded " + str(doubles) + " doubles", end="\r")

def fsog():
    global doubles
    while True:
        if (rand1 == rand3 and rand2 == rand3):
                if converthex not in fiftylist:
                    fiftylist.append(converthex)
                else:
                    doubles += 1
                truerandom()
                if len(fiftylist) == 50:
                    print(*fiftylist, sep = ", ")
                    break
        else:
            truerandom()

fsog()
→ More replies (1)

0

u/[deleted] Nov 23 '20

[deleted] — view deleted comment

1

u/asaf92 Nov 23 '20

Also, this version can be solved with Matlab which is always fun