9 ms·
random(random(random(random())))
- maxbond 3y agoIf you're wondering why the dots approach the circumference and not the center, the length of the vector is `diameter * (1 - random(random(...)))` It's interesting that the second circle is a pretty uniform distribution. `1 - random(random())` must be approximately `sqrt(random())`, iirc that's how you correct for the error demonstrated in the first circle (https://youtube.com/watch?v=4y_nmpv-9lI https://youtube.com/watch?v=4y_nmpv-9lI).
- deleted 3y ago[deleted]
- bonzini 3y ago... That means each point is at a distance of sqrt(f(x)) from the circumference. And nesting random() n times is similar to random()^n, which explains why the first plot has them amassed in the center and the second has a more uniform distribution. Why are they similar? For a rough idea, random() is a random number between 0 and 1, which on average is 0.5; random(random()) is a random number between 0 and on average 0.5, which on average is 0.25; and so on.
- ozfive 3y agoOh man if my math teachers taught math with application, I would have been so much better off. It's so much easier to understand all of this after writing code for a while.
- contravariant 3y agoThe cumulative distribution of random(random(...)) repeated k times is 1-(1-x)^k, so 1 - random(random()) should have a cumulative distribution of x^(2). I think that should even make it exactly uniform, but somehow it doesn't look like it. I may have missed a bit somewhere.
- maxbond 3y agoPerhaps it looks nonuniform because we're not using literal points (our dots have nonzero area), so when they get too close they blend together and make the distribution look denser than it is in that area
- contravariant 3y agoCould be, but it is also possible I'm missing something somewhere, especially because I can't explain why min(random(), random()) looks different from random(random()) or random()*random().
- awegio 3y agoAccording to the internet what you wrote is the distribution of min(random(), random()), but the distribution of the product is different. Product: https://math.stackexchange.com/questions/659254/product-distribution-of-two-uniform-distribution-what-about-3-or-more/ https://math.stackexchange.com/questions/659254/product-dist... Minimum: https://stats.stackexchange.com/questions/220/how-is-the-minimum-of-a-set-of-iid-random-variables-distributed https://stats.stackexchange.com/questions/220/how-is-the-min... I can't explain it either, but I also don't think there is a reason they should look the same.
- penteract 3y agoAn attempt to explain the difference without just calculating the things: random(random()) has an identical distribution to random()*random() (it may even behave identically for a given rng state), although this is different to Math.pow(random(),2) since in that case there's 100% correlation between both parts which makes the expected value product bigger. random(random()) is also distributed equivalently to x=random() y=random() while(y>=x) y=random() return min(x,y) (the last line could also read 'return y') Comparing that to min(random(),random()), we can see that if the second call to random is smaller than the first, they will return the same result; otherwise, the program equivalent to random(random()) will return a smaller value, therefore the expected value of random(random()) must be lower that that of min(random(),random()).
- avodonosov 3y agoA better title would be "1 - Random(Random(Random(Random())))", than it's more understandable why the points lean to the circumference.
- nomercy400 3y agoYes exactly. I was wondering why the center was becoming more and more empty when the result was moving more to 0.
- tbalsam 3y agoWhat language? What are the parameters for the random function here (seed? upper range value in a uniform distribution? stddev for a random distribution?) Why? What is this demonstrating? I can grok a lot of the dimensional effects of randomness but without these things specified, the picture is rather meaningless to me.
- maxbond 3y ago1. The language is Processing 2. The argument is the high end of the range (0 to n [https://processing.org/reference/random_.html https://processing.org/reference/random_.html]) 3. I dunno why, it seems like it's just cool. It seems to demonstrate that random(random(...)) collapses to 0, which is exactly what I expected but it's pretty cool.
- deleted 3y ago[deleted]
- tbalsam 3y agoI'm a little confused then what the value of this demonstration is. The expectation marches by half the distance towards the outer edge each time, so it becomes a soap bubble at a rate of 1/2^n Which is nice and sorta cool but I'm not quite sure what the "whoa" factor here is. It's like one of those zip cinch tops for those bags. Sorta cool how it works on observation, really cool in detail, but also sorta...meh? At the same time? Maybe I'm a bit jaded here? Admittedly, a continuous animation of this would be an improvement on the cool factor to me, personally.
- deleted 3y ago[deleted]
- maxbond 3y agoWell, all I can tell you is that I'm having a good time discussing the nitty gritty of it in the comments here, and it made the gears of my mind turn in a pleasant way. It didn't make me go, "wow," but it did make me go, "hmm...."
- paulddraper 3y agoSorry to be blunt, but what is of interest here? Aesthetics? P.S. In case anyone is confused, the arg to random is the upper limit (not the seed), and r = diameter * (1 - value)
- andybak 3y agoMost things are interesting.
- edrxty 3y agoYeah the code under the </> tab is very important, on first blush it looks like random isn't a uniform distribution for some reason. It's just because they plotted it with polar coordinates. It would be much clearer what's actually going on here if an XY plot was used.
- maxbond 3y agoIt is uniform I'm pretty sure, that's what plotting a polar coordinates look like when you use a uniform distribution for the length.
- paulddraper 3y agoNo, it's because random(random(random())) is iterating the upper limit, not the seed.
- qingcharles 3y ago
- gnramires 3y agoTips: The source code can be seen by clicking on '</>'; The random() function takes an upper limit of a desired uniform distribution (from 0 to x), this shows the effect of the distribution of those iterated random functions. I think this isn't so intuitive because the polar coordinates already imply greater density near center (which could be corrected with a suitable map).
- acjohnson55 3y agoThe reason it's counterintuitive is because the randomly chosen distance variable is 1 minus the nested randoms. Each random call becomes the upper bound of future random calls, so the more times you nest it, the closer to zero you're likely to be. Subtracting that from one puts you close to the unit circle.
- em-bee 3y agoright, if the points are distributed in a square then the distribution looks more like what one would expect: (i am to lazy to register to make a fork, here is the code change): replace: let a = random(Math.PI*2); let r = d * (1 - random()); point(cos(a)*r, sin(a)*r); with: let x = random(d); let y = d * (1 - random()); point(x, y); in every block you could even simplify it to: let x = random(d); let y = random(d); point(x, y); and for more fun apply the nested random() call to both axis: let x = random(random(d)); let y = random(random(d)); point(x, y);
- SamBam 3y agoYup, that's way more intuitive, and removes the artifact of the first circle looking like the points are clustered around the middle: I forked it: https://openprocessing.org/sketch/1929579 https://openprocessing.org/sketch/1929579
- msuvakov 3y agoI forked it with similar idea: https://openprocessing.org/sketch/1929687 https://openprocessing.org/sketch/1929687
- romwell 3y agoIt's almost criminal to post things like that without any annotations. The first picture plots points where radius and angle (in polar coordinates) are uniformly distributed. Of course, this is not uniform in the disc. I don't have the time right now, but if I do later, I'll type up the math behind what a uniform distribution in the disc looks like, and what's going on in other pictures.
- deleted 3y ago[deleted]
- klyrs 3y agothe bigger crime is that this is plotting 1-Random(Random(Random(Random()))) and not Random(Random(Random(Random())))
- fsckboy 3y agojust in terms of pure randomness (which is not what this is testing), random(random()) doesn't make more sense than random(). If your random number generator is good, one is enough. If it's not good, multiple times is not going to help. I'm putting this in the past tense, it randumb enough times for me.
- the_af 3y agoThe argument to random() in this case is the upper bound, so `random()` and `random(random())` are different. See: https://processing.org/reference/random_.html https://processing.org/reference/random_.html
- thriftwy 3y agoI like using Math.min(nextInt(), nextInt()) for non-uniformly distributed random data.
- deleted 3y ago[deleted]
- rjbwork 3y agoJust whipped this up in Linqpad real quick to see how quickly random(random(...)) tends to converge to 0. Fun lil script. https://media.discordapp.net/attachments/519790546601115649/1107773267479109742/image.png https://media.discordapp.net/attachments/519790546601115649/... EDIT: Realized I cut off the full script. First two lines are: var ran = new Random(); var counts = new Dictionary<int,int>();
- davesque 3y agoSince `random(x)` returns a random number in [0,x), it will always output a number that is less than `x` (the initial value of x is 1 in this case). Therefore, the outcome where repeated applications of `random(...)` to its own result has little shrinking effect is vanishingly small. It's the result of these repeated applications that is being subtracted from the length of the unit vector that is used to position each pixel. So the pixels naturally accumulate around that unit length. No broken crypto here. It's sort of like asking, "What would happen if I multiply a bunch of numbers together that are guaranteed to be less than 1 (and even much less than 1 much of the time)?"
- gabereiser 3y agoExactly, like Perlin noise, the output is not indicative of the function. Works as intended. The beauty is in the fact that when using pseudorandom number generation and the right output function, you can simulate a lot of things. Star systems, planets, stock market games, aim handicap, and itano circus.
- selcuka 3y agoThat's how many computer games generate their universes. It's called procedural generation [1]. One of the earliest and most famous examples is Elite [2]: > The Elite universe contains eight galaxies, each with 256 planets to explore. Due to the limited capabilities of 8-bit computers, these worlds are procedurally generated. A single seed number is run through a fixed algorithm the appropriate number of times and creates a sequence of numbers determining each planet's complete composition (position in the galaxy, prices of commodities, and name and local details; text strings are chosen numerically from a lookup table and assembled to produce unique descriptions, such as a planet with "carnivorous arts graduates"). [1] https://en.wikipedia.org/wiki/Procedural_generation https://en.wikipedia.org/wiki/Procedural_generation [2] https://en.wikipedia.org/wiki/Elite_(video_game) https://en.wikipedia.org/wiki/Elite_(video_game)
- gabereiser 3y agoI know, I wrote a procedural universe simulation in 2006. It’s like playing god.
- chkas 3y agoShameless copied: https://easylang.dev/ide/#run=textsize%203%0Afor%20i%20%3D%200%20to%2010000%0A%20%20%20a%20%3D%20randomf%20*%20360%0A%20%20%20r%20%3D%2020%20*%20(1%20-%20randomf)%0A%20%20%20move%2025%20%2B%20cos%20a%20*%20r%2078%20%2B%20sin%20a%20*%20r%0A%20%20%20circle%200.1%0A.%0Amove%2017%2055%0Atext%20%22random%22%0Afor%20i%20%3D%200%20to%2010000%0A%20%20%20a%20%3D%20randomf%20*%20360%0A%20%20%20r%20%3D%2020%20*%20(1%20-%20randomf%20*%20randomf)%0A%20%20%20move%2075%20%2B%20cos%20a%20*%20r%2078%20%2B%20sin%20a%20*%20r%0A%20%20%20circle%200.1%0A.%0Amove%2060%2055%0Atext%20%22random%20random%22%0Afor%20i%20%3D%200%20to%2010000%0A%20%20%20a%20%3D%20randomf%20*%20360%0A%20%20%20r%20%3D%2020%20*%20(1%20-%20randomf%20*%20randomf%20*%20randomf)%0A%20%20%20move%2025%20%2B%20cos%20a%20*%20r%2028%20%2B%20sin%20a%20*%20r%0A%20%20%20circle%200.1%0A.%0Amove%205%205%0Atext%20%22random%20random%20random%22%0Afor%20i%20%3D%200%20to%2010000%0A%20%20%20a%20%3D%20randomf%20*%20360%0A%20%20%20r%20%3D%2020%20*%20(1%20-%20randomf%20*%20randomf%20*%20randomf%20*%20randomf)%0A%20%20%20move%2075%20%2B%20cos%20a%20*%20r%2028%20%2B%20sin%20a%20*%20r%0A%20%20%20circle%200.1%0A.%0Amove%2050%205%0Atext%20%22random%20random%20random%20random%22%0A https://easylang.dev/ide/#run=textsize%203%0Afor%20i%20%3D%2...
- jtsiskin 3y agoThis would be way clearer as a Cartesian plot with the output of random as X, and sample number as Y.
- muti 3y agoFork wrapping the random radius in sqrt() so the first disk is uniformly distributed https://openprocessing.org/sketch/1929418 https://openprocessing.org/sketch/1929418
- dathinab 3y agoIt's a funny one. On the first look it looks that random is broken then you realize, no random(n) is a random value between [0;n) so it's behaving exactly right. It also shows really nice how "naively" choosing random points in a non rectangular shape can easily lead to a non uniform distribution.
- CyberDildonics 3y agoIt's still a uniform distribution of points, it's being warped by converting to a non uniform space and back to a uniform space.
- hgsgm 3y agoIt's not a uniform distribution of points after the warping. I'm pretty sure every bounded distribution over a compact space is uniform under some warping.
- hot_gril 3y ago> Provides interestingly titled code example "random(random(...." > Accidentally(?) shows nonuniform-looking distribution of points from uniform random() due to polar coordinate conversions > Shows effects of nesting random calls in a way reminiscent of security papers showing design flaws, but it's not really showing that > Refuses to elaborate further The more I think about this, the better it is. The imperfection is what makes this little bit of code so interesting to us nerds, while even trolling some people, but we have no idea what the intent was. It's great.
- deleted 3y ago[deleted]
- SideQuark 3y agoLots of math all over the place computing the expected value :) So, here's a derivation: if rand(x) returns uniformly a real in [0,x) or equivalently for expected value, in [0,x], then here are the first few iteration expected values from which you can see the general answer. 1 term E(r) = (1/1) * Integral_0^1 x_0 dx_0 = (x_0^2/2) from 0 to 1 = 1/2 2 terms E(r(r)) = (1/1) * Int_0^1 (1/x_0) Int_0^x_0 x_1 dx_1 dx_0 = 1/4 3 terms: E(r(r(r))) = (1/1) * Int_0^1 (1/x_0) Int_0^x_0 (1/x_1) Int_0^x_1 x_2 dx_2 dx_1 dx_0 = 1/8 And so on. The nth term is exactly (1/2)^n
- version_five 3y agoI think I understand, but I also saw a comment above that specifically says (having done 3 iterations) The resulting distribution is not equal to random()^3, because e.g. the probability that all 3 random calls give you a number <0.5 is only 0.125. https://news.ycombinator.com/item?id=35954931 https://news.ycombinator.com/item?id=35954931 Any thoughts? Am I mixing up two unrelated things?
- leapis 3y agoThe re
- version_five 3y agoVery well explained, that made it click for me, thanks!
- NoToP 3y agoEven simpler, every time you sample random you get the same number of bits of entropy. Sampling once and then cubing spreads the same entropy bits over the interval. Cubing thrice spreads 3x the entropy into the reachable area. Thus you know this guy is doing no net favor to the world.
- alpark3 3y agoThere's a difference between taking a random() and then cubing it, and taking three different random()s and multiplying them together. The EV of the first is 1/4, whereas the EV of the second is 1/8. You can prove the EVs for yourself, but an intuitive way to think about why the EV of the first is twice as much as the second scenario is to consider their probability density functions. When you cube a single random variable, the resulting PDF is skewed towards lower values, but not as dramatically as when you multiply three separate random variables together.
- Fatnino 3y agoI see nothing but a light blue screen in Android Firefox. Can someone describe what this is supposed to do?
- utunga 3y agoThis would've been a lot less confusing if they'd used let radius = d * random(random(random())) instead of let radius = d * (1 - random(random(random()))) But, if so, probably so much less confusing that folks wouldn't be talking about it here, to be honest.
- alhirzel 3y agoYep, just about what one would expect from randomly cutting the top of your uniform distribution off multiple times! What's interesting is the transforms used to sample in some new space from e.g. uniform random inputs. For instance, disks [1] or hemispheres [2] or an arbitrary density function [3]. Common stuff in light transport simulation, and easy to get wrong. [1]: https://stats.stackexchange.com/questions/481543/generating-random-points-uniformly-on-a-disk https://stats.stackexchange.com/questions/481543/generating-... [2]: https://alexanderameye.github.io/notes/sampling-the-hemisphere/ https://alexanderameye.github.io/notes/sampling-the-hemisphe... [3]: https://en.wikipedia.org/wiki/Inverse_transform_sampling https://en.wikipedia.org/wiki/Inverse_transform_sampling
- vsnf 3y agoIn Warcraft we call this deathrolling, and it’s a fun way to gamble away all your gold.
- em-bee 3y agocan you elaborate on that please? how do you apply this topic to a game?
- _9za9 3y ago[flagged]
- DontchaKnowit 3y agoCan we please ban this kind of comment on HN? Anyone here who is curious could have used chatgpt for themselves. This doesn't really contribute anything. Since the OP can be assumed to not know what he's talking about (since they had to use chatgpt to explain) we can assume that they also could not fact check chatgpt's answer.
- _9za9 3y agoI disagree. The "ChatGPT: " signifies the potential for inaccuracy and I would have appreciated someone answering so I don't have to. I wonder how many repeat lookups happen because of a perceived societal issue with providing AI answers that leads to groups shadow banning them. Imagine if I didn't put "ChatGPT :" how many people here would have upvoted me instead of flagging me. Interesting lessons all around.
- DontchaKnowit 3y agoI will say, I appreciated the preface.
- tymscar 3y agoIn World of Warcraft, there are text-based commands that can be executed in the chat interface, similar to command line tools in a computer terminal. The "/played" command, for example, will give you the total time spent playing on a specific character. One such command is "/roll". When this command is followed by a number (n), it generates a random number between 1 and n. This is akin to a digital dice roll, with the number specifying the number of sides on the dice. For instance, you might decide to start a game with another player where you both start with a number of 500 and wager 100 gold. You would initiate the game by typing "/roll 500" into the chat, which would then generate a random number between 1 and 500. Let's imagine it gives you 137. The other player then uses this number as the argument for their roll. So they would type "/roll 137". This process continues with each player using the result of the previous roll as the new argument for the "/roll" command. The game ends when a player rolls a 1, and that player is deemed the loser.
- deleted 3y ago[deleted]
- hot_gril 3y agoKinda cool. A little feedback: - random(x) made me think x was the seed. Maybe call it random(0, x) to make it clear to most that you mean min 0 max x. - The random() probably looks nonuniform to most people. Looking at the code, it's a random angle and length in polar coordinates, with the angle not getting any additional random() wrapping in the other examples. XY coordinates would be clearer.
- dheera 3y ago> `random(x)` made me think x was the seed. Fair, but that would be bad design in my opinion. In general you want the random() function to be usable multiple times to get the same sequence after setting the seed once, and not have to specify the seed every time.
- hot_gril 3y agoYeah, neither assumption makes much sense. Seeding should be a separate func, and range random should take two args. Just for some reason seed is the first thing that comes to mind. But now I see this page has editable code and whatever language it's in does have random(x) taking one arg. So fair enough.
- antonvs 3y agoIt’s in Processing. Its random function can take one arg (the max) or two args (the range). So what you suggested would be possible.
- ambyra 3y agorandom(random()) seems to have the most even distribution. Idk why?
- em-bee 3y agoit's an illusion created by the polar plot. the most even distribution is in the first image. in a polar plot, two points that are eg 5degrees apart appear closer together in the center than they appear at the edge of the circle, so the center looks denser even though the angular distance of the points is the same
- pimlottc 3y agoIt’s because it doesn’t really make sense to plot the results of random() in a circle like this. There’s the same number of points with radius between 0 and 1/2 as between 1/2 and 1, but they have to fit in a space with 1/3 the area as the outer band. So it’s naturally more dense. The fact that random(random()) /looks/ more even in this visualization shows that the actual distribution is not.
- fnord77 3y agowhich I could fork on this site without creating an account
- zootboy 3y agoIn a related vein, there's a Numberphile / 3B1B video about the pitfalls of plotting randomness in a circle: https://www.youtube.com/watch?v=mZBwsm6B280 https://www.youtube.com/watch?v=mZBwsm6B280
- quickthrower2 3y agoI was hoping it was the seed! That it is the max is less interesting
- chaosprint 3y agoRandom is always interesting. Here is an example I made to use logistic map for music note generation: https://glicol.org/demo#chaos https://glicol.org/demo#chaos
- pietroppeter 3y agoLove that openprocessing is being featured here! (I checked and this is the first big hit on HN). It is a great open source coding site with great coding artists, who share their creations openly. My personal favorite is Okazu, see an example here: https://openprocessing.org/sketch/1653811 https://openprocessing.org/sketch/1653811
- FabHK 3y agoFAQ 1. So what’s happening here? Conceptually, this goes around the circle in steps of 1 degree, say, and for each ray plots a point at a distance from the circumference given by random(), or random(random()), etc. So, conceptually, For phi = 1..360 r = 1 - random() // etc. x = r*cos(phi) y = r*sin(phi) (It might choose phi randomly uniformly instead, doesn’t matter) 2. What’s random(random())? As explained in detail elsewhere, since the argument specifies the upper limit of the uniform, it equals random()*random() (ie the product of two independent uniform 0-1 random variables). 3. Why is the first image denser in the center, and the second uniform? Imagine cutting the disk into 100 rings of equal (small) thickness. The area of a ring is approximately thickness times circumference of the ring, in other words, proportional to the distance from the center. If we put the same number of points into each ring, the rings near the center (with less area) will have more points per area than the rings at the outside, thus appear darker. If, however, we put more points into the outer rings, and in particular if we make the number of points proportional to the distance from the center, then we will get a more uniform picture. 4. Why 1-random(), rather than random()? With the latter, we’d just get a series of disks with the bulk of the points concentrated in the centre, increasingly so. Less interesting than what’s here.
- HornyDude 3y agoAs a way of illustrating the distribution of a set of random numbers, it's an absolutely terrible visualization.
- j7ake 3y agoI think there is a link with curse of dimensionality here. As you add more randoms, most of the points lie close to the surface. This is also the case in high dimensional spheres: most of volume lie near the surface of the hyper sphere.
- locuscoeruleus 3y agoThis was my immediate thought as well! Can someone more mathematical say if this is an unreasonable thought?
- tysam_and 3y agoIt depends upon what metric you use to measure it. Of course with Euclidean it will as that seems to match our expectations for low dimensional space. I'm not a mathematician so there is not much more that I could say on the matter at this juncture.
- j7ake 3y agoThe volume of an n-ball has been calculated for other non-Euclidean distances. For p goes to infinity, the unit "ball" becomes a "cube" of two units wide per side. https://en.wikipedia.org/wiki/Volume_of_an_n-ball#Balls_in_Lp_norms https://en.wikipedia.org/wiki/Volume_of_an_n-ball#Balls_in_L... https://www.johndcook.com/blog/2010/07/02/volumes-of-generalized-unit-balls/ https://www.johndcook.com/blog/2010/07/02/volumes-of-general...
- tysam_and 3y agoMany thanks, I've been wondering about this one for quite a while. <3 :))))
- j7ake 3y agoAs one increases more dimensions, it becomes increasingly likely that at least one of them will be an "extreme" value (i.e. point is at the edge of the hypercube ). In the random() * random() * random() ... , once you draw at least one number close to 0 (chances will increase as you add more randoms), then your output collapses to a very small number. And in this visualization, 0 is shown as a point at the edge of the circle.
- rightbyte 3y agoWouldn't a square be less confusing than a circle for a uniform distribution?
- thih9 3y agoWhere "random" is defined as: let a = random(Math.PI*2); let r = d * (1 - random()); point(cos(a)*r, sin(a)*r); (click "</>" in the submission to view source).
- nicexe 3y agoFrom what I understand (and creatively assume) the concentration in the middle (on the top left panel) is because of mapping random (uniform distribution) in Cartesian space to polar space. Not sure I can explain the effect on the other panels since the first iteration is already based on assumptions.
- freetime2 3y agoThanks I was curious about the concentration in the middle!
- deleted 3y ago[deleted]
- nikhilsindhu00 3y agoI'm intrigued by it