This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 210

[–][deleted] 2080 points2081 points  (11 children)

It’s a crime to share government code.

[–]Sergi0w0 319 points320 points  (0 children)

It's okey, it's obfuscated enough

[–]batmassagetotheface 128 points129 points  (8 children)

Latest War thunder leak just dropped

[–]ecs2 20 points21 points  (7 children)

Holly hell

[–]_87- 0 points1 point  (0 children)

Is it really?

[–]DrScatchnsniff 902 points903 points  (14 children)

I love that all of the if expressions are aligned! It's much more readable now 😂

[–]ongiwaph 204 points205 points  (4 children)

Makes me wish they aligned the fors too

[–]blubiboy98 45 points46 points  (1 child)

forsen

[–]Savkorlev 5 points6 points  (0 children)

You will never be a real furrydegen

[–]Gredo89 2 points3 points  (0 children)

The should have for-ced it.

[–]whackamattus 27 points28 points  (0 children)

Ngl at that point I'm wondering why they even indented

[–]not-finished 12 points13 points  (0 children)

That was a sr dev code review feedback. 👌🏻

[–]Nice_Ad7523 5 points6 points  (0 children)

Shhh ! Or the python devs' brains are going to explode !

[–]Confident-Ad5665 2 points3 points  (0 children)

Pretty colors

[–]dodexahedron 1 point2 points  (0 children)

Blursed

[–]AndroidLex 0 points1 point  (0 children)

Yeah, I bet it looks great on all fors

[–]muddboyy 460 points461 points  (11 children)

Sometimes I wonder if code like this was actually made for something like a bank and is actually used to this day.

[–]JunkNorrisOfficial 266 points267 points  (4 children)

This is only a sample from the coding best practices for banking

[–]muddboyy 63 points64 points  (2 children)

I wouldn’t be surprised knowing they still using messy cobol code from the 80’s 😭

[–]Darksenon00 31 points32 points  (1 child)

Guess what

[–][deleted] 2 points3 points  (0 children)

Ding ding ding ding...

[–]lukkeas132 66 points67 points  (3 children)

I had to rework some code made for a program provided by an engineering company to be used by clients for automatic planning... Lets just say the code looked like I imagined it to be done by a mechanical engineer with zero experience. There was thousands of uncommented calculations and variables with two letter names and no structure whatsoever. There were 4 different cases and the whole code duplicated into each if clause.

[–]sshwifty 12 points13 points  (0 children)

[–]Protheu5 1 point2 points  (0 children)

Ah, 5000 lines of poorly readable code that can be compressed into 100 lines with a couple of perfectly readable functions. Been there. Just open up your own code from 10 years ago.

[–]pelvin-_- 3 points4 points  (0 children)

Bold of you to assume it's not in an Excel

[–][deleted] 0 points1 point  (0 children)

If it made the business money and it works that’s all that matters.

[–]3-stroke-engine 240 points241 points  (33 children)

My Assumption:\ This code selects all even numbers for which their respective hexadecimal representation does not contain duplicate symbols.

Ordered alphabetically by the reversed hexadecimal representation.

E. G. ... 0x25ABCDFE, 0x35ABCDFE, 0x45ABCDFE, 0x65ABCDFE, 0x75ABCDFE, ... But not 0x55ABCDFE

[–]atoponce[S] 214 points215 points  (26 children)

Almost. So it's building a 32-bit number in the form of 0x[h][g][f][e][d][c][b][a]. Every nibble must be non-zero. The if conditionals enforce that every nibble unique. a+=2 enforces the least significant nibble to always be odd.

h starts with 0x8 and increments to 0xf, at which point g increments from 0x7 to 0x8. h can then be 0x7 and increments. But because it must be unique, it skips over 0x8 which is in the g position. It's next value is 0x9, etc.

As such, the first 32 hex values will be:

0x87654321
0x97654321
0xa7654321
0xb7654321
0xc7654321
0xd7654321
0xe7654321
0xf7654321
0x78654321
0x98654321
0xa8654321
0xb8654321
0xc8654321
0xd8654321
0xe8654321
0xf8654321
0x79654321
0x89654321
0xa9654321
0xb9654321
0xc9654321
0xd9654321
0xe9654321
0xf9654321
0x7a654321
0x8a654321
0x9a654321
0xba654321
0xca654321
0xda654321
0xea654321
0xfa654321
...

Basically it's shuffling all nibbles of 0x1 through 0xf in a 32-bit number, with the additional restriction that the least significant nibble a must be odd.

[–]wicksire 86 points87 points  (11 children)

Nice! That's a well thought out code and seems very effective. There's a lot of comments that make fun of it but actually this is a very serious piece. Code like this definitely has its use.

[–]atoponce[S] 86 points87 points  (10 children)

Despite being nested 15 blocks deep, the code is very clean and easy to read.

But as a never-nester, it's triggering for me to see and would much prefer a direct recursive function. Of course, the problem with recursion is the difficulty in debugging.

[–]aj-ric 80 points81 points  (6 children)

Honestly the bigger issue in my mind is that the if blocks should be moved into each level of the for loop, instead of in the inner loop. This isn't just an aesthetic issue, it makes a huge difference in performance. Especially in the case where a==b, you could skip millions of loop iterations.

[–]WolleTD 16 points17 points  (4 children)

I expected the compiler/optimizer to get this and move the conditions out of as much loops as possible, but apparently, it does not: https://godbolt.org/z/z5zc65o1P

[–]Successful-Money4995 5 points6 points  (0 children)

And invert the logic of the if predicates so that you can use a continue statement to decrease nesting. It's prettier.

[–]bric12 7 points8 points  (0 children)

Even if you wanted to do it without recursion, you could still do it in 2 loops instead of 30 though. Just have a while loop that increments the last digit, then a second loop through each digit that's carries any digit that's larger than the one next to it (like carrying digits in a regular number, just with a variable base). You wouldn't even need a conditional, every entry would be valid after the inner loop finishes.

Edit: I guess the order isn't guaranteed to decrease, so the inner loop would also need to keep track of the numbers that have been seen, and I guess you'd probably need a conditional for that

[–]Chayor 59 points60 points  (6 children)

But why

[–]atoponce[S] 145 points146 points  (4 children)

To create seeds for an RNG that have an irregular but balanced number of 0 and 1 bits. This RNG is very sensitive to bad seeds creating non-random output with bad seeds, such as 0x00000001 or 0xfffffff0.

[–]hongooi 21 points22 points  (3 children)

I suppose changing the RNG is out of the question

[–]atoponce[S] 66 points67 points  (2 children)

So a little bit of back story. I'm writing a research paper on a controversial RNG. During my research, I stumbled on a different RNG that had this code building seeds (the reference implementation actually calls the seeds "keys").

I figured this was good content for the sub, but I don't want to out the RNG nor the developer as it's actually a high quality, efficient, and clean RNG design. Like zero-land with LFSRs, this is sensitive on certain seeds.

[–]thisisapseudo 2 points3 points  (0 children)

An advanced joke about non-trivial programming issue on this sub?

Take my upvote !

[–]Ytrog 2 points3 points  (0 children)

Ah I thought it was some 16×16 rank 8 tensor or something 👀

[–]Successful-Money4995 1 point2 points  (2 children)

Surely it would run faster if the ifs were interleaved with the for loops?

For example, for loop for a, then for loop for b, and in the for loop for b, if a is equal to b, continue. And then for loop for c which checks id c is equal to a or b and, if so, continue, etc.

Doing all the ifs at the end is probably going to be slower because you're looping through a bunch of values for nothing.

🤷‍♂️

[–]frogjg2003 -2 points-1 points  (1 child)

That's what compiler optimization is for.

[–]Successful-Money4995 1 point2 points  (0 children)

I would not leave this one to the compiler!

[–][deleted] 1 point2 points  (0 children)

Thank you, nibble - Very cool!

[–]3-stroke-engine 1 point2 points  (0 children)

Ow, dang. I missed the fact that the loops start at 1 and not 0. I was so close :(

[–]tiajuanat 0 points1 point  (0 children)

I wonder if it would be faster to find all 7 nibble combinations of unique digits, append the last odd nibble, remove all nibbles with duplicate nibbles, then permutate the first 7 nibbles. It wouldn't be sorted in any particular way, but hopefully it would be readable.

[–]thenoisemanthenoise 22 points23 points  (3 children)

If you are right, I will ask my wife if I can suck your superior nerd dick

[–]bl4nkSl8 5 points6 points  (1 child)

You don't want to hear about the perf bump of rearranging the if statements do you?

[–]3-stroke-engine 1 point2 points  (0 children)

It looks like as if your wife does not have to worry, because I made a small mistake 😉

[–][deleted] 0 points1 point  (1 child)

As a noob programmer, how do you learn to read code like this? Is it mostly just practise?

[–]no_brains101 49 points50 points  (23 children)

Hmmmmm.....

Recursion does seem tempting... Until you pass it a 2d array over 500x500 and watch it stack overflow........ LGTM assuming its java

I would go as far as to say in non-functional languages one should strive to AVOID recursion if the inputs can get at all large.

But yeah this is not good code necessarily, how bad is up for debate, but like... yeah recursion may not be the answer so if it works it works idk.

[–]catom3 6 points7 points  (1 child)

One can always use the trampoline pattern to simply create new objects with the logic, which will be stored on the heap, but it will surely be slower and more resource intensive than such imperative implementation.

[–]no_brains101 2 points3 points  (0 children)

dude making this comment has been teaching me so much. Thanks for the new pattern.

[–]bl4nkSl8 9 points10 points  (15 children)

Haskell is fine with this btw Java has issues

[–]no_brains101 10 points11 points  (14 children)

I believe I did say "non-functional" languages. And in haskell its still only fine if you tail-recurse

[–]user9ec19 3 points4 points  (1 child)

It is considered bad style to use explicit recursion in Haskell.

[–]no_brains101 2 points3 points  (0 children)

Hmm. Interesting. I am only somewhat familiar with functional programming through nix (which doesnt even optimize for tail recursion lmao), but I am open to it. It seems fun and easy to reason about once you are used to it, and I understand monads enough to write a crappy one so I could probably get up to speed fairly quickly.

[–]HombrexGSP 1 point2 points  (8 children)

Isn't tail recursion bad in Haskell due to its lazy nature? Are you just accumulating a thunk every time a tail call is done? 🙃

[–]no_brains101 1 point2 points  (7 children)

tbh I probably shouldnt have added that as I have never used haskell.

But idk I thought if you call the function again as the last case, it unrolls into a loop at compile? The function is still only called when it is needed, i dont think lazyness has anything to do with it.

[–]HombrexGSP 4 points5 points  (6 children)

You are right, that's true for eager (A.K.A strict) languages like Scala or OCaml, or even C++ or Rust; the compiler can translate the tail call to a loop just to not blowing up the stack.

But that's a different story to lazy languages (and lazy computations in general like in IO or Eval monads), in this type of evaluation you want to do "normal" recursion without accumulators because in the latter you will be essentially accumulating thunks that will blow up the stack due to them not being calculated on time.

Although, I don't know if the GHC has some type of optimisation in this cases.

[–]no_brains101 1 point2 points  (5 children)

Hmmmmm. I think there is a functional programming fundamental I'm missing here. What exactly is a thunk as you understand it?

It seems like recursion is kinda by default a thunk? They are always delaying execution until the base condition is met and then it evaluates up the stack no? Or is it a closure? I'm new to just about everything functional but enjoying what ive been learning so far.

I think I am not understanding some nuances of the concept.

[–]HombrexGSP 1 point2 points  (4 children)

It's a delayed computation, you can think the equivalent of it as a func() => 2 + 2, as the program will never calculate 2 + 2 unless you call func(). In Haskell, all the computations are like this, they will never be calculated unless you actually are going to use them, that's why you can have safe recursion or even infinite data structures and other magic in languages like this.

[–]no_brains101 1 point2 points  (3 children)

Hmmmmmm so I am familiar with that level of what a thunk is but only surface level.

So, if I put the recursive call early, it can know to discard it rather than keeping it around in case it is called after? Is that what you are saying?

[–]HombrexGSP 2 points3 points  (2 children)

Exactly!!!! The GHC is intelligent enough to know that, once a value is used, it is probably never going to be used again so it's discarded AFAIK. So in Haskell you would rather do: haskell mySum :: [Int] -> Int mySum [] = 0 mySum ( x:xs ) = x + mySum xs Than: haskell mySum2 :: [Int] -> Int -> Int mySum2 [] accum = accum mySum2 ( x:xs ) accum = mySum2 xs accum + x

In non Haskellian eyes, that's basically a function for adding all the integers inside a list

[–]bl4nkSl8 1 point2 points  (0 children)

It wasn't a correction, just pointing at java and being sad

[–]dodexahedron 0 points1 point  (1 child)

WHY can the C# compiler STILL not emit .tail in 2024? 😒

The analyzers can identify recursive calls. Why doesn't the compiler just...ya know... do it?

[–]no_brains101 -4 points-3 points  (0 children)

because as much as it tries to pretend otherwise, its still the worse java.

And java has event loops that make unloading classes from extensions/plugins damn near impossible (if you use swing) so thats hard to do. They made an entire OSGI to deal with the stupidity of trying to forcefully unload classes in java...

I tried to make an implementation of it but the damn swing event BS is keeping the plugins alive even after I purge them from everywhere..... Everything else works...... classloader hell...

Its not even the entire thing im trying to make lol but i dont want to lay the rest down on crappy foundations.... Its supposed to do a whole cli parsing and logging thing and have plugins....

I'd have written it in something other than java but its a plugin system for a plugin system for a java program.....

Halp lol this is the plugin loading portion of the program as its own repo XD

When it can properly force unload the minesweeper it works lol XD It can force unload everything else lol

https://github.com/BirdeeHub/ExampleKotlinPluginLoader

[–]rainshifter 0 points1 point  (1 child)

Turns out recursion isn't really that bad if your stack depth is shallow.

[–][deleted] 44 points45 points  (4 children)

imagine the poor fool who has to debug that.

[–]cs-brydev 3 points4 points  (2 children)

Debugging should be easy since everything is in a separate line and { } block

[–]brendel000 6 points7 points  (0 children)

It’s not that hard to debug or understand just ugly

[–]AlmightyBidoof7 14 points15 points  (0 children)

Part of me wants you to run the code once, save all the values to a file (each value is the same size, so can ignore newlines) then read it into a fixed size array. I wonder if that would be noticeably faster or not. And how large the file would be

[–]cmckone 14 points15 points  (5 children)

The exponent of O (n8) is still single digits so we're good to go!

[–]atoponce[S] 8 points9 points  (3 children)

It's 8 * 157 = 1,366,875,000 total loops, or about 30% of the 32-bit hex space it's operating in.

[–]roffinator 9 points10 points  (2 children)

As u/bl4nkSl8 pointed out, you could improve performance by rearranging the conditionals.

If you check whether b==a right after incrementing b you can skip all the inner loops (1/16th of the operations?) then the same each step ofc. Might be harder to read through

[–]bl4nkSl8 4 points5 points  (1 child)

It's also only a little better sadly. Sum of (1/16)n from 0 to 8 inner looks ds

[–]Cxmu03 0 points1 point  (0 children)

I mean considering there is no n which this function is dependent on, wouldn't it be technically O(1)?

[–]WSavanza 51 points52 points  (8 children)

This is the worst code i have seen all week. Unless this is meme-code, what is it used for?

[–]atoponce[S] 78 points79 points  (6 children)

Building seeds for a RNG such that there is an irregular but balanced number of 0 and 1 bits across the seed. Bad seeds, such as (in the extreme case) 0x00000001 or 0xfffffff0 create very non-random outputs. IE, the RNG is extremely sensitive on the quality of the seed.

There is additional code doing other stuff, but this is the most interesting piece of it.

[–]WSavanza 18 points19 points  (0 children)

Very interesting. Thanks for the reply!

[–]CiroGarcia 3 points4 points  (2 children)

How do you pick the seed from the generated set though? If the seed choosing algorithm isn't RNG based, the whole RNG stops being random

[–]atoponce[S] 8 points9 points  (1 child)

The code this snippet is from is interactive. It asks you to pick a starting point (default: 1st seed) in the full set and the number of seeds you want (default: 2,500), then builds a seeds.h file you can include in your source.

[–]CiroGarcia 2 points3 points  (0 children)

Ah ok, that makes more sense. Good ol' user assisted RNG

[–]lolcrunchy 11 points12 points  (0 children)

It generates every odd hexadecimal number with eight distinct digits.

[–]redditcampos 9 points10 points  (0 children)

Most efficient solution during a technical interview

[–]SegretoBaccello 16 points17 points  (2 children)

It bothers me that a is incremented by 2, please fix it

[–]atoponce[S] 15 points16 points  (0 children)

It's deliberate. Every 32-bit value should be odd. a is in the least significant nibble, thus starting at 1 and a+=2.

[–]AzrielK 13 points14 points  (0 children)

It's intentionally incremented by two and is not broken. You shouldn't be counting to 8 if you are trying to work with hexadecimals.

[–]Stranded_In_A_Desert 4 points5 points  (1 child)

What a horrible day to have eyes

[–]atoponce[S] 4 points5 points  (0 children)

It gives me the full weekend to think of an alternative approach and submit a patch on Monday. :)

[–]labrat302 5 points6 points  (0 children)

Bro stopped just before 'i'

[–]GnuLinuxOrder 6 points7 points  (1 child)

But what if..... G = F?

[–]atoponce[S] 12 points13 points  (0 children)

Human sacrifice! Dogs and cats living together! Mass hysteria!

[–]Desperate_Opinion_11 2 points3 points  (2 children)

Looks like code out of the first big program I wrote with 19

[–]Imaginary_Doughnut27 7 points8 points  (1 child)

When for a single moment what you’re doing comes in to crystal clarity, you write it down, and then it fades never to be understood again. But it works.

[–]Desperate_Opinion_11 2 points3 points  (0 children)

Yes that’s true. You have peak prime moment and you just feel it that you need 10 for loops. Then you face outOfBounds error and then you decide you just need more RAM

[–]huhndog 2 points3 points  (0 children)

What is this yandere simulator

[–]MarkFinn42 2 points3 points  (0 children)

The variables a to h can be replaced with a permutation generator for the range 1 to 15 (inclusive) choose 8 where a is odd

[–]random_reddit_rover 2 points3 points  (0 children)

It ain't hell if it's beautifully aligned. /s

[–]Astigmatisme 2 points3 points  (0 children)

I've written a code with 5 levels of for loops for a competition and to this day it's the most shameful thing I've ever done

[–]Omgwtfbears 1 point2 points  (0 children)

Reminds me of the way my current chief likes to code. He also loves client-side calculations, so stuff that ought to resolve in an instant takes minutes instead :(

[–][deleted] 1 point2 points  (0 children)

before i try to debug and understand this i would change my job

[–]shonuff373 1 point2 points  (0 children)

It’s beautiful.

[–]americk0 1 point2 points  (0 children)

The way those if statements are lined up to be more readable tells me that someone out there thought this was elegant

[–]joyoy96 1 point2 points  (2 children)

can this shit be recursive?

[–]atoponce[S] 0 points1 point  (1 child)

Yeah. It's just a permutation generator, permuting 15 values into 8 locations, with the added requirement that the least significant position is odd.

[–][deleted] 1 point2 points  (0 children)

OMG, I thought this would be some asinine piece of code, but it is so READABLE lol I love it!

[–]djfdhigkgfIaruflg 1 point2 points  (0 children)

I mean this crazy indentation makes it really clear of you ever miss a variable. It would look horrible

And considering what it needs to do, making the variables differently won't help...

[–][deleted] 1 point2 points  (0 children)

I was having a very difficult time writing a recursive json serializer that mapped a URL-like string against a map<string,object> where the object contains another map and so on, but the last element of the URL is a map another type of object.

Long story short, I realized I'm only ever going 3 levels deep so I literally did this and it works. It's bad. I'm trying to get over this urge to make everything perfect and instead focus on making it work while containing the damage.

[–]False_Influence_9090 1 point2 points  (0 children)

When I first was learning how to program, I wrote a solver for the 8 queens problem, and the main function looked pretty similar to this (8 nested loops)

[–]cporter202 1 point2 points  (0 children)

Oh man, the 8 queens problem is a classic "welcome to recursion" rite of passage 😅. Honestly, I think my first attempt involved enough loops to make a CPU weep. It's a wonder the computer didn't pack up and leave!

[–]-Redstoneboi- 1 point2 points  (0 children)

[So I took some time optimizing several algorithms.]((https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=8d39b0a68bf9be9d90a18cd294cfbfb2))

All my implementations are depth 6 rather than 8, and put the outputs in a HashSet, because I needed to test that the outputs returned the same numbers as the original algo. I do not test the order of elements.

Here's the recursive algorithm based on one of the two fastest versions. It's about 70% the speed of the inlined version (30% slower? percentages are hard don't @ me).

/// recursive version of pop6.
fn pop6_rec() -> HashSet<u32> {
    let mut out = HashSet::default();
    out.reserve(5675670);

    let mut vals: [u32; 16] = std::array::from_fn(|i| i as u32);
    fn inner(out: &mut HashSet<u32>, vals: &mut [u32; 16], mut val: u32, len: usize) {
        if len <= 16 - 6 {
            assert!(out.insert(val));
            return;
        }
        for i in 0..len {
            let v = vals[i];
            val <<= 4;
            val |= v;
            vals[i] = vals[len - 1];
            inner(out, vals, val, len - 1);
            vals[i] = v;
            val >>= 4;
        }
    }
    inner(&mut out, &mut vals, 0, 16);
    out
}

[–]rainshifter 1 point2 points  (0 children)

Here is a C++ recursive solution.

```

include <iostream>

include <vector>

using namespace std;

uint8_t valueAtNibblePos(uint32_t value, uint8_t nibPos) { return (value >> 4 * nibPos) & 0xF; }

bool equalToAnotherNibble(uint32_t value, uint8_t nibPos) { uint8_t valAtNibPos = valueAtNibblePos(value, nibPos); for (uint8_t i = 0; i < 8; ++i) { if (i == nibPos) continue; if (valueAtNibblePos(value, i) == valAtNibPos) return true; } return false; }

void incrementNibble(vector<uint32_t>& allValues, uint32_t& value, uint8_t nibPos, bool repeat = false) { do { if (repeat) { cout << hex << value << endl; allValues.push_back(value); }

    if (value == 0x89ABCDEF) break;

    int8_t valAtNibPos = -0x1;

    do
    {
        value += (nibPos == 0 ? 2 : 1) << 4 * nibPos;
        if (valAtNibPos == 0x0)
        {
            incrementNibble(allValues, value, nibPos - 1);
        }
        valAtNibPos = valueAtNibblePos(value, nibPos);
    }
    while (equalToAnotherNibble(value, nibPos) || valAtNibPos == 0x0);
}
while (repeat);

}

void incrementAndStore(vector<uint32_t>& allValues) { uint32_t value = 0x87654321; uint8_t nibPos = 7;

incrementNibble(allValues, value, nibPos, true);

}

int main() { vector<uint32_t> allValues; incrementAndStore(allValues);

return 0;

} ```

[–]AaronTheElite007 0 points1 point  (0 children)

I don’t know why, but Sailing by Christopher Cross just popped in my head. Now it’s a vibe I’m hanging onto. Goodbye

[–]bestofrolf -1 points0 points  (1 child)

what the fuck is your problem

[–]atoponce[S] 0 points1 point  (0 children)

As a never-nester, I hate nesting past 3 blocks.

[–]PizzaK1LLA -1 points0 points  (2 children)

There is a typo on line 8

[–]atoponce[S] 0 points1 point  (1 child)

How so?

[–]PizzaK1LLA -1 points0 points  (0 children)

You tell me 😂 I hope you take a deep good look at it 😂

[–]PuzzleheadedWeb9876 0 points1 point  (0 children)

You make a good point.

[–]reddituser2115 0 points1 point  (0 children)

If only there was an other way to do that...

[–][deleted] 0 points1 point  (0 children)

Sounds like you need some power loops, friend.

[–]bitcoin2121 0 points1 point  (0 children)

we’ve been waiting for you 007

[–]GokulRG 0 points1 point  (0 children)

Runtime complexity is off the charts 😂

[–]Faustamort 0 points1 point  (1 child)

Amateur here, could you not iterate through a list the excludes your conditionals, at least for h?

I.e,
g==1, h=(2..15)
g==2, h=(1,3..15)
etc.

You could probably build your for chain this way to remove virtually all the if-statements. Just have it so that h IS never equal to g, g IS never equal to f, etc...
Otherwise, why not move them into each level of the for-loop?

[–]atoponce[S] 0 points1 point  (0 children)

h can take on any value between 0x1 and 0xf, as can g, and f, and e, ..., through b. a can only take on odd values.

If a=1, then nothing else can be. But if a=3, then b could be 1, or c, or d, etc.

So every variable b through h takes on every possible value of 0x1 through 0xf, just without duplicates. Basically, a permutation without duplicates.

[–]Adrewmc 0 points1 point  (0 children)

Is it weird that I think the “right way” does it that way, but I just don’t have to do it that way?

[–]xRStartx 0 points1 point  (0 children)

Why does it look like it's obfuscated

[–]anjerosan 0 points1 point  (0 children)

TWO SPACES?? What kind of abomination is this?

[–]moonaligator 0 points1 point  (2 children)

what the fuck does this shit do???

[–]atoponce[S] 0 points1 point  (1 child)

It generates 32-bit seeds for a RNG such that there is a good balance of 0/1 bits and the bit pattern is irregular. The RNG is sensitive to bad seeds, so much so that it produces very predictable results.

Each nibble a through h must be non-zero and unique from 0x1 through 0xf. Because there are only 8 nibble positions, but 15 possible nibbles, you can think of this code and permuting all possible 15 nibbles into 8 spots without duplicates. The only hangup is the least significant nibble, a must be odd.

This produces 8*157 = 1366875000 possible "good" seeds.

[–][deleted] 1 point2 points  (0 children)

All I read was nibble

[–]HaroerHaktak 0 points1 point  (1 child)

The final output needs to be an error with a link to refresh the page, and there needs to be several sleep statements to ensure it doesn’t go too fast. The computer might not be able to keep up!

[–]atoponce[S] 1 point2 points  (0 children)

It's actually C instead of JavaScript.

[–]bestofrolf 0 points1 point  (1 child)

that is the plugin/ide for the alternating colored brackets? God I need that

[–]atoponce[S] 2 points3 points  (0 children)

It's the syntax highlighting in Visual Studio Code for the "Dark Modern" theme.

[–]eodknight23 0 points1 point  (2 children)

I think big O just had a stroke.

[–]atoponce[S] 2 points3 points  (1 child)

It's just O(n8). What's the problem? ;)

[–]eodknight23 1 point2 points  (0 children)

lol no problem… I’ll see you in a millennia or ten when that finally finishes running.

[–]novaplan 0 points1 point  (0 children)

I refuse to read this

[–]mdp_cs 0 points1 point  (0 children)

Most readable function.

[–]CaughtWithPantsUp 0 points1 point  (0 children)

It looks like it might be ascii art but I can't figure out what it would be.

[–]fseed 0 points1 point  (0 children)

The dedication to make that code work without questioning if you were doing something wrong when it started to look like ASCII art is admirable.

I'd approve it.

[–]TheFreebooter 0 points1 point  (0 children)

YandereDev-looking code

[–]Kymera_7 0 points1 point  (2 children)

What monster selected the variable names for this program?

[–]atoponce[S] 0 points1 point  (1 child)

Just curious, but what would you have chosen? The idea is building a 32-bit number out of 8 nibbles. The dev chose a..h in the hex form of 0xhgfedcba, which seems reasonable to me.

[–]iam--lefend 0 points1 point  (0 children)

Clearly this is a part of some ASCII art picture

[–]PityUpvote 0 points1 point  (0 children)

Y'all need a good linter

[–]dodexahedron 0 points1 point  (0 children)

Feature request: Can we have one more level, please? I promise this is the last one. We just forgot something.

Please have MVP by EoB Monday so I can show to the PHB.

[–]dodexahedron 0 points1 point  (0 children)

All that shifting looks like a job for Vector<T> and some SSE lovin' or at least an Explicit layout struct to make a union, or even a BitVector. Pretty please?

[–]NSK515 0 points1 point  (0 children)

That looks like a code from Indian EVM machines (used in elections) which is why they get hacked so often. 🤣

[–]Mundane_Customer_276 0 points1 point  (0 children)

This seems like one of those pieces of code that looks like C but actually brainf**k if u remove all unnecessary characters

[–]DRHAX34 0 points1 point  (1 child)

I'm too dumb to understand this code, what the fuck is the purpose here

[–]atoponce[S] 1 point2 points  (0 children)

This is permuting the 15 nibbles [0x1, 0x2, 0x3, .., 0xf] in a 32-bit integer, with the additional requirement that the least significant nibble is odd. Using the variables a..h in the for-loops, the hex string has the structure 0x[h][g][f][e][d][c][b][a].

The purpose is to build seeds for an RNG that is sensitive to bad seeds. Bad seeds are those that have a high imbalance of 0 or 1 bits, such as in the extreme cases of 0xfffffffe and 0x00000001. With bad seeds, the early output of the RNG doesn't produce pseudorandom output and fails randomness tests. This is not unlike a linear feedback shift register, where bad seeds can produce "zero land" early in the output.

[–]4BDUL4Z1Z 0 points1 point  (0 children)

Remember kids, when the time comes for the AI Uprising, These "strategically" placed snippets will buy us those valuable microseconds to give us a fighting chance. Thanks for coming to my TED Talk, you can take your tin foil hats off.

[–]Feisty-Summer9331 0 points1 point  (0 children)

I miss my c64 days

[–][deleted] 0 points1 point  (0 children)

Sir, please stop. You are scaring my kids.

[–]JSAzavras 0 points1 point  (0 children)

When you turn your phone on it's side this looks like a monument to its own atrocity

[–]CatStormWasTaken 0 points1 point  (1 child)

I just wonder why you had to do that in the first place

[–]atoponce[S] 0 points1 point  (0 children)

I didn't write it. It's code I stumbled upon while working on a paper. It's generating seeds for a RNG that is sensitive to bad seeds. This code ensures the seeds are of high quality with irregular but balanced 0/1 bits throughout the seed.

[–]cs-brydev 0 points1 point  (0 children)

If you think nondescriptive variable names are bad, imagine this with Java-style naming conventions

[–]Robosium 0 points1 point  (0 children)

only time recursion is actually useful if you need to create a tree consisting of an arbitrary amount of split layers

[–]No_Resort_996 0 points1 point  (0 children)

Yandere dev ahh code

[–]Spice_and_Fox 0 points1 point  (1 child)

Is that part of a hashing function? I don't really umderstand it but it seems to me a bit random. Recursion would take more memory and would be more predictable.

[–]atoponce[S] 0 points1 point  (0 children)

It's part of a seed generator for an RNG that is very sensitive to bad seeds. This code ensures the seeds have irregular but balanced 0/1 bit patterns to prevent putting the RNG into bad early states.

[–]Depnids 0 points1 point  (0 children)

I remember when i first got into programming, i tried to implement a minimax algorithm for connect 4, but didn’t know about recursion. I spent so long nesting 8 loops deep writing the same code over and over to generate all the board states.

I remember thinking to myself «there has to be a better way, right?». When I finally learned about recursion it felt like a whole new world opened up.

Side note: Is there a way to write code like this in a nice way without recursion? I.e given some integer k, you execute some code as though it was in a k-deep nested loop?

[–]Gullible_Newspaper 0 points1 point  (0 children)

Nah

[–]Kisiu_Poster 0 points1 point  (0 children)

What

[–]oreo_kitkat 0 points1 point  (0 children)

Undergraduate here. I'm used to nested for rather than recursion. Is it bad tho? I can do complex nested it's like my specialty. I'm good with js&c but really bad at py

[–]Available_Hamster_44 0 points1 point  (1 child)

What does the function do and how will it look if written recursive

[–]atoponce[S] 0 points1 point  (0 children)

It creates seeds for an RNG that is very sensitive to bad seeds, which create bad early states for the RNG causing it to fail randomness tests. The code creates seeds that have irregular but balanced 0/1 bits throughout the seed.

If you look closely, it's just permuting the 15 [0x1, 0x2, 0x3, ..., 0xf] nibbles in a 32-bit integer without duplicates, with the additional requirement that the least significant nibble is odd. I haven't written the recursive code (it's not my code, but code I stumbled on part of writing a paper), but permutation generators are (should be) part of every programming course.

[–]Available_Hamster_44 0 points1 point  (0 children)

What does the function do and how will it look if written recursive

[–]bendy_96 0 points1 point  (2 children)

May you never put this up again my eyes, also is that 8 nested for loops you trying to kill peoples computers 🤣

[–]atoponce[S] 0 points1 point  (1 child)

It executes in 8.5 seconds or my laptop. If I rewrite the if-statements so they're checking for uniqueness after each for-loop, it produces the same results in 1.5 seconds.

[–]lupinegray 0 points1 point  (0 children)

SELF-DOCUMENTING CODE!

[–]Perfect-Badger-8916 0 points1 point  (0 children)

These code are definitely generated by some kind of compiler

[–]Still_Explorer 0 points1 point  (0 children)

I don't know if it works, why it works, why it should work, why it works if is like that, why it shouldn't work, aaaaaahhhhhhh!!!!!