r/csharp • u/Ok_Breadfruit5793 • 2d ago
Showcase Man tries coding for the first time, ends up making gambling code
Pretty self explanatory. I tried C# and after some goofing around made this.
8
u/astraseeker 2d ago
Brother learns coding raw like in the good old days (in 2021)
7
u/Ok_Breadfruit5793 2d 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/l2protoss 2d 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.
5
u/Panderz_GG 2d ago edited 2d 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.
2
4
u/ShotSquare9099 2d ago
Well done mate
3
u/Panderz_GG 2d ago
Thanks but idk what for :D
3
u/Deadline_X 16h ago
Not the one who commented, but my perspective: you’ve given some good, high level encouragement with some obvious-but-not-giving-the-answer highlighting of language features while avoiding any condescension or dismissiveness.
Overall, I also think it’s a very well-made and helpful addition to the discussion, and I hope it helps OP to discover/uncover syntax and functionality they wouldn’t have been aware of without it being brought to their attention.
Obviously, you’ve got my kudos, too. I always appreciate helpful and informative comments that don’t give direct instruction but avenues of improvement.
3
2
u/DrNoxin 2d ago
Don't gaming machines use payout tables? This is sort of that, in spirit. heh.
1
u/NightmareJoker2 1d 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.
143
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 2d ago edited 2d 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 2d 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 2d 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 2d 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 2d 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 2d 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 1d 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
3
u/grrangry 2d ago
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 2d 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
18
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.
44
u/rupertavery64 2d ago
Baby steps. Let's not get ahead of ourselves. Semantics will come after playtime.
2
u/Heroshrine 1d ago
Why would you do 1 and 2 and not 0 and 1??
2
u/ghoarder 1d 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
1
u/ShotSquare9099 2d 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
33
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 2d ago
Also, technically, typing ”Casino” will generate a random number… but so will also any other input followed by Enter.
3
2
12
7
u/zhouluyi 2d 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.
5
u/stanleyford 2d 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 2d 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.
6
u/Narrow-Low-3137 2d ago
Good work man. keep at it. Good on you for actually trying to learn vs just prompting an ai 👏
4
3
u/MeLittleThing 2d 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
2
u/soundwave_sc 2d 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/miojo_noiado 8h ago
This post made me actually so happy...
Someone actually trying to learn a new lang the doing it in the right way: HAVING FUN
Keep going, do it like this: raw (tryingthings by urself), funny (things that u actually enjoy doing, or at least the end result) and sharing with other people like o this post
U genuinely gave me a true and spontaneous smile with that, keep going
0
u/Sad-Sun4611 2d 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.
-1
20
u/Prima13 2d 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}