Jump to content
The King of Hate Forums

Cool Coding Tricks


Recommended Posts

I was looking at some things regarding coding, and learned some cool stuff, so I decided to mess around with it, (specifically in Python). This thread is just for posting simple but interesting programming syntax, (and better if you explain it), but just for fun, nothing too complicated or serious.

So with this one, you can estimate π with basically a number randomizer and by applying a formula after recounting the numbers. The idea is that if you randomize dots in a semi-uniform manner inside an overlapped & centered square & circle that have the same diameter/edge length, the dots that fall within the circle divided by the ones that fall in total, (so inside the square, and therefore inside the circle), should approximate (π/4), without you needing to input π at all.

If you run this in Python, it should give you the estimate, with obviously a different result each time, and more accurate to the number the more iterations you give it, (here I put 1000000 times):

import random
def approx_pi(z):
    dot_in_circle = 0
    dot_in_total = 0
    for _ in range(z):
        x = random.uniform(0,1)
        y = random.uniform(0,1)
        rad = x**2 + y**2
        if rad <= 1:
            dot_in_circle += 1
        dot_in_total += 1
        
    return 4 * dot_in_circle/dot_in_total

print(approx_pi(1000000))

I didn't know about this until recently, I think it's pretty cool.

Edited by MoraMoria
  • Upvote 1
Link to post
Share on other sites
1 hour ago, GuessWhat said:

Cool. I didn't know you were into programing.

Just a bit.

8 hours ago, babymngaming456 said:

There's actually simple math behind it.

The point of this is to estimate π with a randomizer between numbers [0 and 1, included], and then applying a formula after recounting.

You have to do it without having π, or the area of an already existing circle or square, (or circumference), that's why you use the randomizer. In your equation, you already have either of those, so it's pointless, you already have the solution.

Your highlighted equation at the bottom of the image is already in the return statement, but "A_circle" was calculated with the help of the randomizer.

Link to post
Share on other sites
10 minutes ago, MoraMoria said:

The point of this is to estimate π with a randomizer between numbers [0 and 1, included], and then applying a formula after recounting.

I know what the point is.

11 minutes ago, MoraMoria said:

In your equation, you already have either of those, so it's pointless, you already have the solution.

The point is to show how the formula works.

Link to post
Share on other sites

This is something I did a while ago, but is related to programming.

You can actually make an encoder & decoder of ciphertexts in Microsoft Excel, although it's not going to look super fancy. The one I did that's shown here, just encodes the plaintexts to Beaufort & Vigenère, and below it, a decoder of those two as well. You just enter the plaintext & key, (the only thing you do in this version is enter the data in the green-cells).

It works by assigning a number to each letter of the alphabet, (made it for English), and then you just apply a formula to add or substract them & output the corresponding letter, depending on the cipher. But because it's Excel, you have to take a lot of things into consideration if you want to make it work, like making sure that the key loops correctly regardless of its length, and that the output for the result also assigns the correct letter no matter the length of the string, (with a limit for the one I made).

One day I'll try it with a programming language instead of Excel, and I'm sure that some parts will be easier.

Crypto.thumb.png.e70a1e99a830a265e4c3ff4cdb59ee4a.png

Edited by MoraMoria
  • Upvote 1
Link to post
Share on other sites
  • 3 weeks later...

I've done a bunch of things in Excel that I want to do with a programming language.

Like with this, it basically shows a letter-frequency-analysis of the English language, (in the: "% English" row, according to some sources), and in the top one it counts the specific letters of the text you input, and colours it, according to its frequency. The result shows that the colours seem to match fairly well, so the text should have a decent chance of being written in English.

So if I make this a bit more complex, it might work good with Caesar-Ciphertexts as well:

TextAnalysisEX.thumb.png.084f740db61a65e3c0833b48cf9421c4.png

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...