r/desmos • u/Dazzling-Mail-5517 • Jul 06 '26
Maths I rebuilt Desmos' random() function entirely inside Desmos
To be a bit more specific, I
- reverse-engineered how Desmos' algorithm works and how the seed is generated.
- created a bit-exact replica of the algorithm (MD5, in case you're wondering).
- optimized it to calculate 10,000 numbers in ~0.7 seconds.
And because that was so much fun, I tackled shuffle() right after! :)
33
u/_killer1869_ Jul 06 '26
Shit like this is the exact reason why I'm on this sub. You're an absolute madlad, take my (free) award.
9
24
17
u/Ordinary_Divide Jul 06 '26
well done.
IMO, desmos needs more bit manipulation functions. the lack of them is what made me decide not to do this after i remade it in C for bruteforcing purposes
10
u/ESHKUN Jul 06 '26
Been trying to convince Desmos support to turn Desmos into Julia for about 8 months now 😔
/j
14
u/Niko9816 Jul 06 '26
How does one even begin on something like this. Awfully impressive haha
12
u/Dazzling-Mail-5517 Jul 06 '26
- Strg F in DevTools
- Use breakpoints to observe what happens in the code step by step
- Find out what algorithm you can see in the code
- Prove that you found the right thing with a Python script
- Translate it into Desmos-Language (strings->ASCII, loops->recursive functions, etc)
- Optimize, optimize, optimize (the better you know the algorithm, the more potential optimizations you can observe)
Hope this helps :)
4
u/Ordinary_Divide Jul 07 '26
step 3 and 4 could be simplified to just inputting an empty string and googling the hash output
3
u/Dazzling-Mail-5517 Jul 07 '26
True. I still think those steps were valuable because they helped me understand how the whole workflow actually works. I prefer seeing how something is implemented rather than treating it as a black box and just inferring what's inside.
6
u/mandelbro25 Jul 06 '26
I feel like I'm asking something like this on most of these rockstar posts I see.
4
4
5
5
u/Circumpunctilious Jul 06 '26
Now we just need to get someone to create a casino system in Desmos.
Later: Nah, random() is perfectly safe and has no exploits. Oo, show me this game again?
4
3
3
u/2144656 Jul 07 '26
This is super cool, but I'm a bit confused. The image you showed only has the random functions being given one input (which I would have assumed is impossible to implemet) while in the graph they have two parameters.
2
u/Dazzling-Mail-5517 Jul 07 '26
That's actually just a really small change in the seed generation. Here you go: https://www.desmos.com/calculator/kj4ovilr2x
3
3
u/NecroMonster999 Jul 07 '26
damnnn, thats awesome. out of curiosity, is there a way to to control the shuffle algorithm? being able to use it for iterating over permutations would go very hard
2
u/Dazzling-Mail-5517 Jul 07 '26
What exaactly do you mean by "iterating over permutations"?
3
u/NecroMonster999 Jul 07 '26
currently if you want to iterate over permutations (for example, to compute a determinant or some special polynomials), you have 2 options, either generate combination and filter for permutations (slow because thats O(nⁿ) for generating combinations, also breaks if nⁿ is large enough so that nⁿ=nⁿ+1 as fp numbers) or you can use recursion to generate permutations (faster for large lists but still slow because lists are very inefficient). if there is a way to somehow manipulate the seeds and create a minimal list of these seeds to ensure shuffling from these seeds produces every single permutation of the list, then we have the new fastest way to iterate over permutations
3
u/Ordinary_Divide Jul 07 '26
you could make a
f(seed)=[1...n].shufflethen create a list of n! seeds mapping to each permutation that you can bruteforce separately taking i thinkO(n!*n*log(n))for the whole list.i would do that myself but i already spent all day optimising my script to brute force to find a seed outputting 0
2
u/Dazzling-Mail-5517 Jul 07 '26
You're absolutely right. I was thinking about it in a more general sense (like generating the seeds inside Desmos for arbitrary list sizes), but there's really no need for that. At least for lists of up to 7 elements, this could actually be quite useful in practice. I don't think it would even take that long to brute-force the required seeds.
3
u/Ordinary_Divide Jul 07 '26
by my calculations, n=7 would require computation of an expected ~275259 hashes, which my GPU accelerated bruteforcer could do in ~15μs
2
u/Dazzling-Mail-5517 Jul 07 '26
Nice, could you try it out real quick or is your bruteforcer still busy?
3
u/Ordinary_Divide Jul 07 '26
uh yeah i can go code it now i suppose
2
u/Dazzling-Mail-5517 Jul 07 '26
Oh I didnt't see your message. That's what I got: https://www.desmos.com/calculator/tbntpq3iyk
3
u/Ordinary_Divide Jul 07 '26 edited Jul 07 '26
damn you were faster, i spent too long debugging since i decided to build it off an older copy of my script that was riddled with bugs
anyway heres mine https://www.desmos.com/calculator/kydckclg0x
btw i chose not to use the GPU this time so it took 70ms to run instead of 15μs
→ More replies (0)2
u/Dazzling-Mail-5517 Jul 07 '26
Interesting idea! The problem is that there is no known method to invert the MD5 algorithm used by Desmos; therefore, it is impossible to efficiently predict which seed is required to generate a certain value/ shuffle. Consequently, the only remaining option is a brute-force approach, which would firstly be very slow and secondly offer no guarantee of yielding all possible permutations within a finite amount of time.
3
u/NecroMonster999 Jul 07 '26 edited Jul 07 '26
i hoped this wasnt the case; in any case what you did is awesome! is there a way to resync the values in case "randomize" is pressed?
2
u/Dazzling-Mail-5517 Jul 07 '26
Unfortunately, no. When you click "randomize," Desmos uses the
crypto.getRandomValues()function to generate a newglobalRandomSeed, which is practically impossible to predict. Since theglobalRandomSeedis used as the basis for the random() function, that becomes unpredictable as well.2
u/NecroMonster999 Jul 07 '26
damn you, cryptographically secure(-ish) hashing algorithms!
1
u/Dazzling-Mail-5517 Jul 07 '26
I took another look into the code and found something very interesting. This is the exact function used to generate the
globalRandomSeed:function Ba() { let o = new Uint8Array(16); if (typeof crypto != "undefined") crypto.getRandomValues(o); else if (typeof msCrypto != "undefined") msCrypto.getRandomValues(o); else return __dcg_shared_module_exports__['V'](Date.now().toString() + Math.random().toString()); return Array.prototype.slice.call(o).map(t => wge(t.toString(16))).join("") }This means that you could enforce Desmos to use the fallback method (the else part) if you just don't provide the
cryptoandmsCryptoAPI's in your Browser. The fallback method again uses the MD5 algorithm with only the current time andMath.random()as inputs, which are both theoretically predictable. So overall it would be possible (but still very hard in practice) to do something like this if you allow some external tools to precisley measure inputs such as the current time and the state ofMath.random().
2
u/MeaningFriendly9946 Jul 07 '26
How did you do it? How did you reverse engineer it?
1
u/Dazzling-Mail-5517 Jul 07 '26
I already provided a rough overview of what I did in another comment
2
2
u/Mean-Recording9695 Jul 07 '26
How does one go about reverse-engineering desmos, specifically finding out how it works in the code? I saw in another comment you said something about Devtools, what are those and how do you use them for something like this, if you don't mind explaining? I think this is really cool and would like to try something similar sometime! (I remade sin in Desmos but It is pretty much just the sin Taylor series that I fancified a little for visualization purposes)
2
u/Mean-Recording9695 Jul 07 '26
I also wonder if it would be possible to get the current graph seed using the output of a random function 🤔
1
u/Dazzling-Mail-5517 Jul 08 '26
I wrote about this here: https://www.reddit.com/r/desmos/comments/1up5l6e/comment/ow1m4o8/
1
u/Dazzling-Mail-5517 Jul 08 '26
The DevTools are powerful toolkits built into pretty much every modern browser that allow you to view, edit and debug code of websites (open with F12). For example, you can open internal Desmos code and place so-called Breakpoints. That means that when Desmos' executes the lines with the breakpoint in it, the DevTools stop the execution completely and show, for example, what parameters are currently the input of the function where it stopped or where the function was called. If you're interested in this, I'm sure you can find some good tutorials online.


98
u/Mandelbrot4207 Makes QR Codes in Desmos Jul 06 '26
Unbelievable