r/csharp • • 2d ago

Showcase Man tries coding for the first time, ends up making gambling code

Post image

Pretty self explanatory. I tried C# and after some goofing around made this.

169 Upvotes

57 comments sorted by

18

u/Prima13 1d ago

Nice job. This is more or less how we all started. In my case it was on a Timex Sinclair 1000 in line numbered BASIC in 1983.

As others have mentioned, your if statement could be reduced to just:

if(randomNumber < 6) {
// do something
}
else {
// do something else
}

17

u/Ok_Breadfruit5793 1d ago

Oh, wow im a fucking idiot, didnt even know else existed.

30

u/sketch_56 1d ago

Nah, you're learning. The "fucking idiot" title is reserved for people who have at least 10 years experience of knowing better but doing worse.

6

u/Ok_Breadfruit5793 1d ago

Thanks man

3

u/Ian__16354 1d ago

As the other commenter said, you are learning so not an idiot at all. Also, programming does weird fucking things to your mind that even after programming for years you will sometimes find solutions like this and not think of anything else lol

3

u/Contagion21 1d ago

My favorite is coming across some stupid code and being annoyed thinking "who wrote this!?" and then feeling quite sheepish after running 'git blame'

4

u/RSGMercenary 1d ago edited 1d ago

There's also else if, and you can do a bunch of them together.

if (...)
    // Do something.
else if (...)
    // Do something else.
else if (...)
    // Do something extra else!
else
    // Do something super else!

3

u/yarb00 1d ago

However it's important to know that else if is not a separate language feature, but just nested if inside else (works because braces are not required for a single statement).

So, this code:

if (x) DoSmth1();
else if (y) DoSmth2();
else DoSmth3();

Means:

if (x)
{
    DoSmth1();
}
else
{
    if (y)
    {
        DoSmth2();
    }
    else
    {
        DoSmth3();
    }
}

1

u/Els236 1d ago

I'm a novice too. Would a switch statement not work here?

int num;

switch num:

etc, then cases for each number

1

u/RSGMercenary 1d ago

You could use a switch too. With improvements to switch expressions and pattern matching, switches can do a lot of what if statements do. I'm sure there's a conversation to be had about performance differences, but I usually prefer ifs when it's more than a single condition/value being checked.

6

u/astraseeker 1d ago

Brother learns coding raw like in the good old days (in 2021)

5

u/Ok_Breadfruit5793 1d ago

I tried to use tutorials but that was boring so ima do my own thang (see you in 2 decades with my first game.)

2

u/IMP4283 1d ago

This is a good attitude to have

1

u/l2protoss 1d ago

For me this was the best way to learn - just pick a thing and start doing it. I would get tutorials and watch until I kinda got the concept and then tried to make something with it. When I got stuck I’d just try to find that topic in the tutorial I had or go to SO.

2

u/Poat540 1d ago

Homie is straight naming vars and all, the good stuff

2

u/Panderz_GG 1d ago edited 1d ago

Very nice, people already talked about the numbers.

Now to make this program a little bit more involved.

What can you do with the string you just got?

You want the User to put in the string "Casino" but your program actually doesn't care what the User writes, even if they put in "Trololo" the program will generate a random number, IF you could find a way to COMPARE strings and let your program respond accordingly, that would be cool. Maybe have it ask for the correct string WHILE the incorrect string is saved in the string variable.

1

u/Ok_Breadfruit5793 1d ago

Huh, will look into

2

u/ShotSquare9099 1d ago

Well done mate

1

u/Panderz_GG 1d ago

Thanks but idk what for :D

1

u/DrNoxin 1d ago

Don't gaming machines use payout tables? This is sort of that, in spirit. heh.

1

u/NightmareJoker2 16h ago

Yes, sort of.

It’s always rigged in favor of the house.

While there is an element of random chance, you can think of it as an advent calendar, where you get to open a random door every time you or anyone else decides play. The machine collects a fee for every attempt, and it pays out ~30% of the fees it collected as winnings. To ensure it never overspends, the big prize that exceeds the entry fee is only paid out once a pre-defined profit margin is reached.

138

u/rupertavery64 2d ago edited 2d ago

Great! Now think about what all the numbers 1,2,3,4,5 and 6,7,8,9 have in common. And think about the operators ==, <, >, <=, >=

How can you describe the numbers 1 through 5 in relation to the randomNumber using one of those operators? Can you write it in such a way that you don't have to write each number?

BTW, what you've done is exactly what everyone who doesn't follow a tutorial does - goof around with code. Have fun. It's a proven way to learn, it's what we do as kids.

4

u/RottenH20 2d ago

(Assuming we dont just make the generator range 1~2)

Would subtracting half of the max (10) from randomNumber (-5) then check if its greater than 0 work?

13

u/rupertavery64 1d ago edited 1d ago

Yes. But code is also about being understandable.

You could say that all inequality operators fundamentally compile to a subtraction between registers (CMP instruction) in the CPU, and then a test for zero / negative flag, i.e. there is no such thing as a greater than / less than instruction.

2

u/RottenH20 1d ago

Could you give an example? Sorry I'm still a junior and I am confused as to how that applies.

Do you mean check if the sign is negative? IE != first bit, or something else?

6

u/rupertavery64 1d ago

Basically, yes. If you're a junior you don't have to worry about what actually goes on when it's compiled to machine code, just focus on writing code that you can understand.

But what you said (subtract 5 from the number and check if greater than 0) is exactly what will happen when you write x < 5 or x > 5.

If you want to know what happens:

This will compile to something like

  • load register A with the value in X
  • load regsiter B with the value 5
  • Compare A with B
  • branch if negative

Compare performs a signed subtraction, and the result will have the leftmost bit set to denote a negative value. The result isn't actually stored anywhere, instead, the N flag of the status register will be set to 1, and succeeding branch instructions (branch if zero, branch if negative) will check the value of status flags in order to perform a branch.

1

u/RottenH20 1d ago

Thanks for the reply. I understand now.

When would you say someone should start to think about the compiled machine code? Or is that something that comes naturally over time?

4

u/rupertavery64 1d ago

Lots of people don't really care. The language abstracts all of that, and different CPUs can implement things slightly differently.

I got into emulation and wrote a couple of emulators so I waded into the lower level stuff.

I also tinkered with microprocessors.

3

u/Pickled-Onion-5710 1d ago

I've been programming for 45 years, I never knew this until now. Nor would it ever have made my code better if I had known it. An interesting thing to find out though.

1

u/NightmareJoker2 16h ago

Where is the bell curve graph meme where on the left it says “focus on writing code that is easy to understand”, in the middle it says “knowing what actually happens has (serious) performance implications”, and on the right side is says “focus on writing code that is easy to understand”? 😅

Because the true answer is architecture dependent, and RISC-V does have them (as does System Z from IBM, but forgive me, I can’t find the reference at the moment).

1

u/sububi71 1d ago

The Carry flag wishes a word.

3

u/grrangry 1d ago

https://learn.microsoft.com/en-us/dotnet/api/system.random.next?view=net-11.0#system-random-next(system-int32-system-int32)

The max isn't 10. The exclusive upper bound is 10, making the max 9.

Random.Shared.Next(1, 10)

returns 1 through 9.

1

u/RottenH20 1d ago

Yes sorry, meant to say the value present. So save 10 to var and pass it into the random function and use same var for var / 2 to get 5

17

u/ghoarder 2d ago

There are two outcomes, why not just get a random number between 1 and 2 inclusive? Like roulette, red or black.

42

u/rupertavery64 2d ago

Baby steps. Let's not get ahead of ourselves. Semantics will come after playtime.

7

u/moocat 1d ago

At least for this example, they are not statistically equivalent.

2

u/Heroshrine 15h ago

Why would you do 1 and 2 and not 0 and 1??

1

u/ghoarder 11h ago

I probably would, but the guy only learnt c# today so didn't want to burden him with 0 based indexing and off by 1 bugs 

1

u/ShotSquare9099 1d ago

Well done mate, This is the first post I’ve seen on this sub and the top two comments are just fantastic. you both guided OP to think about the problem, and encouraged OP to keep going. Great stuff guys

31

u/TheKidfromVillage 2d ago

This kinda reminds me of my early programming days😂. Keep it up. Try building unconventional projects, there you'll end up learning a lot.

7

u/l2protoss 1d ago

Also, technically, typing ”Casino” will generate a random number… but so will also any other input followed by Enter.

3

u/shogunzek 18h ago

Yeah, I immediately went looking for the Casino check

2

u/sweetnsourgrapes 1d ago

a casino for goths?

11

u/CalebAsimov 1d ago

Now you're ready for this obligatory xkcd: https://xkcd.com/221/

3

u/zhouluyi 1d ago

This is funny in so many levels... I miss checking out xkcd

6

u/zhouluyi 1d ago

Some 30+ years ago I had the huge insight of checking for odd or even in an integer, by dividing by two, converting to string and finding a '.' between the numbers.

4

u/stanleyford 1d ago

We have all looked at old code that solved some problem in a novel, but ultimately foolish way and thought, "How could I have written something so dumb and so smart at the same time?"

2

u/Robot_Spartan 1d ago

I've found that the best indicator of personal growth is to look at an old piece of code, think "what idiot thought THAT was a good idea?", only to realise you were in fact, that idiot.

4

u/Narrow-Low-3137 1d ago

Good work man. keep at it. Good on you for actually trying to learn vs just prompting an ai 👏

4

u/Rrrrry123 1d ago

W code, bro, fr fr.

3

u/MeLittleThing 1d ago

Your code is working, that's what matters, especially for an early beginner. It could be greatly improved, of course, but you'll find it yourself over time, with practice and experience

1

u/thygrrr 1d ago

Literally my first random number "generator" was a fixed list of print statements, in an endless loop, that I would interrupt with AMIGA+. ("Ctrl-C")

You're good.

1

u/soundwave_sc 1d ago

This is a good start, but you do know as the house - nothing is ever random.

Try rigging your own outcomes, enjoy the process.

1

u/NecoDev 1d ago

You know, operators that are not == DO exist.

Conditional operators ALSO exist.

0

u/Sad-Sun4611 1d ago

" if random number equals 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9" 

Lol, lmao even.