C
C#2mo ago
vendeten

having problems with random number generator in uni project

I was given a small assignment for a bit of uni project to make a program that generates a random number then asks you to guess. The problem is that after each guess the number gets randomised again, can someone help (I’ve tried putting it outside of any loops and it still does this)
No description
No description
48 Replies
Jimmacle
Jimmacle2mo ago
$paste
MODiX
MODiX2mo ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
vendeten
vendetenOP2mo ago
Alright
Salman
Salman2mo ago
You are not decrementing lives anywhere btw. You need to decrement it if the number guessed doesn't match
vendeten
vendetenOP2mo ago
Yeye, for now I’m just trying to fix the number thing. I’ve rewritten this like 4 times because I keep forgetting to save Acc wait nvm it does
Salman
Salman2mo ago
Can you explain the problem a bit more
vendeten
vendetenOP2mo ago
I’m so tired sorry can barely focus I will I’ll just quickly do this
vendeten
vendetenOP2mo ago
BlazeBin - ksnasrqhcskw
A tool for sharing your source code with the world!
vendeten
vendetenOP2mo ago
It asks you to guess a random number between 1 and 10 If you guess it right you get a message saying you did. If not you lose a life. The only reason it shows the number you need to guess was cause I was seeing why It didn’t work You get the option to play again after finishing
Core
Core2mo ago
So basically a binary search with extra steps
vendeten
vendetenOP2mo ago
I guess yea? Just a thing where a number gets generated and you need to guess it
Angius
Angius2mo ago
I'd use the debugger to see where the code goes and what the values are at each point It seems that it loops back to the outer loop, for some weird reason Debugging would confirm or deny that theory
Core
Core2mo ago
Sorry mb, it isn't a binary search. But I don't see the problem
Angius
Angius2mo ago
Same, can't see the issue at a glance either
vendeten
vendetenOP2mo ago
Huh
Salman
Salman2mo ago
Code works fine for me
vendeten
vendetenOP2mo ago
Weird, asked sir and he said other people were having a similar issue
Salman
Salman2mo ago
Just tried
Angius
Angius2mo ago
Unless it's some ancient version of .NET that had issues with Random...? I don't think so though VS could be running an old version of the binary, but it only does so if the current version cannot be compiled The code seems fine, though, so not that
vendeten
vendetenOP2mo ago
Like what happens is It generates 4 Says “The number has been generated, please guess” 10 I guess 10 Message suddenly got changed to “The number has been generated, please guess” 7 Or smth like that Alright
Salman
Salman2mo ago
I'd just move the random instance creation to the class lever maybe
Angius
Angius2mo ago
You mean you get a new number, just like that, without any inputs?
Salman
Salman2mo ago
That doesn't happen for me when I try
Angius
Angius2mo ago
I'd still debug and see
vendeten
vendetenOP2mo ago
No I input it And then it changes
Angius
Angius2mo ago
Place some breakpoints, step through the code Also, show the terminal output when it happens
vendeten
vendetenOP2mo ago
Alright- Maybe i should also just use like visual studio or smth?
Angius
Angius2mo ago
I'm curious when exactly it happens Yes
Salman
Salman2mo ago
No description
Angius
Angius2mo ago
Visual Studio 2022 Community is recommended
Salman
Salman2mo ago
What are you using if not VS
Angius
Angius2mo ago
If you're using some weird online tepl, that could be it
Core
Core2mo ago
My beloved Rider....
Angius
Angius2mo ago
Well, yeah, Rider is great, but not free alas Well, free for students
vendeten
vendetenOP2mo ago
Smth the uni advised to use because visual studio could have problems
Angius
Angius2mo ago
But
Salman
Salman2mo ago
This is programiz online compilers output
Core
Core2mo ago
Visual Studio only has problem with the C language, it is perfectly fine for C#
vendeten
vendetenOP2mo ago
Oh my god was it just the compiler or whatever I was using
Angius
Angius2mo ago
Could very well be
vendeten
vendetenOP2mo ago
I spent like ages on this At least I know what to use now Thanks everyone
Angius
Angius2mo ago
Anytime :Ok:
vendeten
vendetenOP2mo ago
No wonder I was just thinking “Why the hell os this not working it makes no sense” I’ve done a bit in python but not C# so I was expecting it to be like some logic difference with rng in C#
Angius
Angius2mo ago
The only thing that can trip you up with Random, is if you instantiate a lot of Random instances quickly, they might all give the same value So it's usually better to either instantiate a Random once, or just use Random.Shared
MODiX
MODiX2mo ago
Angius
REPL Result: Success
Random.Shared.Next(0, 100)
Random.Shared.Next(0, 100)
Result: int
71
71
Compile: 303.161ms | Execution: 18.646ms | React with ❌ to remove this embed.
Angius
Angius2mo ago
Not an issue in your case, though
vendeten
vendetenOP2mo ago
Yea yea Both frustrated that it was that simple of an issue but happy I know what to do The work was also due in like 2 months but I just wanna get it done early and have free time
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server