r/javascript • u/got_data • Mar 07 '19
This script predicts your next input and messes with your sense of free will :-0
https://github.com/ex-punctis/not-so-random47
Mar 07 '19
My first try: R R R R R R L R R R R L L R R
Correct guesses: 0
On longer runs it catches up to about 53% for me which is a bit better than just random guesses, I guess :D
13
u/got_data Mar 07 '19 edited Mar 07 '19
I think I know what happens. When the 5-gram buffer is initialized, it contains 01010 or 101010 — sorry, I'm too lazy to look it up :) So when you press your first key, the stats for one of these patterns is updated in the database. The same thing happens for the next 4 keys you enter. So the model becomes trained on this "bad" data, and depending on the inputs, it can end up with 0 guesses as a result. It takes a bunch of keystrokes to drown the "bad" data and make the model more accurate. I did most of my testing with about 200 inputs.
6
u/maxoys45 Mar 07 '19
Doesn't work on my phone? Can't scroll the demo page :/
3
u/got_data Mar 07 '19
I am very sorry about that. Would you mind telling which phone you have? I've tested this script on an iphone, and it worked ok. I suspect a couple of possible issues:
the plotly.js library didn't load and messed up everything
I have a bunch of
.preventDefault();under various events to prevent double-tap zoom when touching the buttons and stuff. Maybe this also messes up scrolling.3
u/maxoys45 Mar 07 '19
Top of the line iPhone 5c :D
3
u/got_data Mar 07 '19 edited Mar 08 '19
I see. I tested it on an iphone 6 (ios 12) before, but I just fired up my old iphone 5 (ios 10), and yeah, no scrolling on this page... I'll try to fix it. Thanks for pointing out the bug!
PS Confirmed
document.ontouchmove = function(event){ event.preventDefault(); }is the issue.I need to replace it with something else to prevent double-tap zooming and general bouncing without disabling scrolling.Edit: bug zapped
2
u/SiLo0815 Mar 07 '19
I also can't scroll. Galaxy S7, Firefox 65.0.1
Edit: Chrome (72) seems to work.
2
4
u/johnyma22 Mar 07 '19
Suggestion. Put an animated gif of it on action in the readme. I found doing this with my projects helped increase usage.
2
u/got_data Mar 07 '19
That's an excellent idea, thank you! Would you recommend any particular utility for recording a gif? I can capture stuff with quicktime and convert, but the quality suffers a bit. I wonder if there are better options out there.
4
3
u/curtastic2 Mar 08 '19
Eh I pressed over 100 times quick randomly and it guessed correctly 50% of the time
1
u/got_data Mar 08 '19
RUN! You're in danger! Google has been looking for a source of True Randomness for a while, and now they are going to capture you and make you a True Random Number Generator. You do not want that!
2
u/curtastic2 Mar 08 '19
Here’s my secret. One time I recorded myself trying to play drums to a song. When I heard it back there will be no semblance of order or beat. That’s what I did I pretended the buttons were drums while the song Bad Romance was was being played in Carl’s Jr.
3
u/alivmo Mar 08 '19
Open console.
pickfn = function (){if ((Math.floor((Math.random() * 10)) % 2) === 0) { $('#left').click() } else { $('#right').click()}; setTimeout(pickfn, 100);}
pickfn()
Profit!
2
u/mwr247 Mar 08 '19
setInterval(_=>$(prediction?'#left':'#right').click());Profit better ;)
3
u/got_data Mar 08 '19
Well, this is exactly what happened in 'The Matrix'
"Do not try and bend the spoon, that's impossible. Instead, only try to realize the truth...there is no spoon. Then you'll see that it is not the spoon that bends, it is only yourself."
2
u/DrDuPont Mar 08 '19
Not that I use jQuery much these days, but somehow this is the first time I've seen logic inside a selector. Nice.
1
u/mwr247 Mar 08 '19
I've spent too much time code golfing. In fact, kicking myself for not saving a byte by removing that useless semicolon >_<
2
u/SlowerThanLightSpeed Mar 08 '19
Tiny suggestion for mobile... Use touchstart to speed up the interface. (onclick forces 300ms delay for event on mobile).
Fun tool. Any theories on why the Aaronson Oracle had better predictive abilities...they claimed 70 to 80% as avg.
2
u/got_data Mar 08 '19
Thanks for the suggestion, I'll implement touchstart. As for Aaronson's Oracle, I can only speculate, but I think there are a few factors at play. First of all, I find the 70-80% interval a bit suspicious — I mean it's one thing not being very random, and it's a totally different thing being that predictable*. So either his test subjects didn't really try their best, or maybe they deliberately fed test patterns to figure out the program's inner workings. Of course that is assuming the numbers quoted are accurate and didn't get inflated (e.g., discounting them by just 5 percentage points makes them look a lot more believable). It's possible that the algorithm was more sophisticated:
There might have been various enhancements on top of that -- e.g., if you're not taking enough data for 5-grams to be useful, then you can also look at 4-grams and 3-grams, and you can also "seed" the predictor with 5-gram data from the previous users (hoping that the next user will be pretty similar).
I think that last point (preserving data from the previous user) was likely to make the biggest difference since my experiments with adaptive n-grams weren't successful.
* The reason I think we are not that predictable is because most of us aren't terribly good at memorizing motoric patterns. For example, I have only elementary piano training, so learning a new piece takes a lot of effort because my fingers keep introducing errors. Learning to inflate and kite a paraglider also took me a while even though its control inputs are almost binary. So when typing random binary patterns, I imagine I also introduce mistakes, and probably a lot more than when learning a piano piece since I'm not actively trying to remember any of the patterns... But of course this reasoning is somewhat iffy since I have absolutely no knowledge of neuroscience... Perhaps mashing keys puts one's brain into a totally different mode.
2
u/zapbeen Mar 09 '19
Over 100, it got 56% and I used a coinflip! Then I tried to do random mashing and it only got 49%
1
u/got_data Mar 09 '19
It happens sometimes even with random inputs. The probability of guessing >=55% correct is ~5% at 200 inputs according to https://www.wolframalpha.com/input/?i=P(x%3E%3D112),+x~B(200,+0.5)
27
u/a_dev_has_no_name Mar 07 '19
I'm pretty random lol 46% $1020 by iteration 130 but I stopped being random and it got the last 10 correct... I'll take my payment in cash, thanks.
・。゚[̲̅$̲̅(̲̅ ͡° ͜ʖ ͡°̲̅)̲̅$̲̅]。゚.*
21
u/Serei Mar 07 '19 edited Mar 07 '19
My guesses are correct 47% of the time (overall)
Memorizing a hundred digits of pi has paid off!
(I'm using the "left if the next digit is even, right if the next digit is odd" trick)
2
u/ciaran036 Mar 08 '19
I couldn't think of something random enough so I selected left or right depending on how close each of the letters in the word 'radiator' were to the letter L. Got 50% guessed correctly!
4
u/hardwaregeek Mar 07 '19
Reminds me of a fencing exercise where one person would attack either left or right (parry 4 or 6) and the other would have to guess and block in that direction. There's a surprising amount of skill in it. Plenty of people fall into certain patterns (alternating too often for instance) and plenty of defenders try to "catch" themselves by switching at the last second (which is worse than just guessing wrong).
4
u/alphabennettatwork Mar 07 '19
My "high-score" was 1027.35 on guess 110... pretty cool and interesting!
3
u/Kernel_Internal Mar 08 '19
function iWin() {
let left = document.getElementById('left');
let right = document.getElementById('right');
let evt = new MouseEvent('click', {bubbles: true, cancelable: true, view: window});
for ( let i = 0; i < 10000; i++ ) {
let rand = Math.floor(Math.random() * Math.floor(100));
if ( rand%2 === 0 ) {
left.click(evt);
}
else {
right.click(evt);
}
}
};3
u/markjaquith Mar 08 '19
Weirdly, knowing that it is detecting patterns means an intelligent actor can beat the machine more easily than if the machine guessed randomly. You can thwart it by predicting which pattern it has learned, and deviating right as it is about to apply it.
It’s like that scene in The Princess Bride.
I found it really entertaining to go slowly and try to trick it. Could keep it in the low-to-mid 40s.
2
2
u/sbk2015 Mar 08 '19
I got Iteration 1163 and its guesses are correct 60% of the time (overall). 806 score remained.
I tried to be random,if I 10101 it quickly catches the rhythm.So I have to constantly changing my pattern,sometimes double tap on one side,sometimes have favor over one side other than the other side,if I'm a robot,I will keep changing ryhthm every 2-5 taps.
2
1
1
1
u/saposapot Mar 07 '19
hahaha, that is amazing. kudos for you pal! I won in a short run so that's another lesson for you kids: quit while you are ahead.
-2
u/SquishyPandaDev Mar 08 '19
I just can't even right now. There nothing special about this; it literally has a 50/50 chance of guessing right. My fucking cat could do this
2
0
u/Syh_ Mar 08 '19
Did it guess 100% for you or something? Why so sour? It's something that was clearly made for fun; something entirely light hearted.
-2
u/SquishyPandaDev Mar 08 '19
Why am I so mad? Because it is basic statics and everyone out here treating like the second coming of Jesus.
1
u/Syh_ Mar 08 '19
Yeah, there's no denying that it's simple. It's not hard to see how it works and how it's biased towards 50% accuracy. But no need to put down somebody's work simply because other people appreciate it.
0
Mar 08 '19
[deleted]
2
u/got_data Mar 08 '19
That can be accomplished with LSTM neural networks (assuming structured text). However, it's a lot heavier computationally, and it requires a fair bit of training to work. But that's basically what we use in smartphones these days. If you are interested in the inner workings of text prediction, I highly recommend taking a machine learning course on coursera or some other mooc platform. Most of them include exercises for predictive typing. Also I recommend taking a look at individual tutorials (google: rnn predictive typing tutorial). It's not difficult to reproduce them if you are somewhat familiar with Python (although getting used to ML frameworks takes some effort). Please be warned: machine learning is highly addictive and time consuming. :)
0
Mar 09 '19
[deleted]
2
u/got_data Mar 09 '19
If you are interested in a very basic mathematical (coding-free) intro into neural nets I recommend 3blue1brown. He talks about the simplest neural nets only, but after that you'll be able to understand higher level videos about LSTM and other architectures.
61
u/got_data Mar 07 '19
Every time it guesses your next input correctly, you lose $1.00, and every time it guesses wrong, you get $1.05. With random choices, the expected value of you winnings is 2.5 cents. But in reality most humans tend to repeat certain patterns, and the script uses it to its advantage, so you end up losing money.
This was inspired by http://people.ischool.berkeley.edu/~nick/aaronson-oracle/