r/csharp • • 15h 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.

100 Upvotes

50 comments sorted by

15

u/Prima13 14h 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
}

13

u/Ok_Breadfruit5793 13h ago

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

21

u/sketch_56 13h 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.

4

u/Ok_Breadfruit5793 12h ago

Thanks man

2

u/Ian__16354 7h 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 7h 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 12h ago edited 12h 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!

2

u/yarb00 11h 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 9h 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 9h 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.

4

u/astraseeker 11h ago

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

4

u/Ok_Breadfruit5793 11h 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 9h ago

This is a good attitude to have

1

u/l2protoss 11h 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 8h ago

Homie is straight naming vars and all, the good stuff

2

u/Panderz_GG 9h ago edited 9h 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 6h ago

Huh, will look into

1

u/DrNoxin 6h ago

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

•

u/ShotSquare9099 19m ago

Well done mate

99

u/rupertavery64 15h ago edited 15h 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.

14

u/ghoarder 15h ago

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

32

u/rupertavery64 15h ago

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

5

u/moocat 13h ago

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

3

u/RottenH20 15h 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?

9

u/rupertavery64 15h ago edited 15h 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.

1

u/RottenH20 15h 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 14h 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 14h 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?

3

u/rupertavery64 14h 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.

2

u/Pickled-Onion-5710 3h 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/sububi71 14h ago

The Carry flag wishes a word.

3

u/grrangry 15h 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 14h 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

•

u/ShotSquare9099 10m 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

20

u/TheKidfromVillage 15h 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.

3

u/l2protoss 11h ago

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

2

u/sweetnsourgrapes 9h ago

a casino for goths?

9

u/CalebAsimov 15h ago

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

3

u/zhouluyi 14h ago

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

5

u/zhouluyi 14h 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.

3

u/stanleyford 12h 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?"

1

u/Robot_Spartan 6h 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 14h ago

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

4

u/Rrrrry123 13h ago

W code, bro, fr fr.

3

u/MeLittleThing 13h 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 14h 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 7h 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.

0

u/Sad-Sun4611 14h 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.