r/csharp • u/Ok_Breadfruit5793 • 15h 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.
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.)
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/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
•
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.
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
3
u/grrangry 15h 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 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
9
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
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/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.
1
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.
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}