C
C#5d 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
Jimmacle5d ago
$paste
MODiX
MODiX5d 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
vendeten5d ago
Alright
Salman
Salman5d ago
You are not decrementing lives anywhere btw. You need to decrement it if the number guessed doesn't match
vendeten
vendeten5d 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
Salman5d ago
Can you explain the problem a bit more
vendeten
vendeten5d ago
I’m so tired sorry can barely focus I will I’ll just quickly do this
vendeten
vendeten5d ago
BlazeBin - ksnasrqhcskw
A tool for sharing your source code with the world!
vendeten
vendeten5d 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
Core5d ago
So basically a binary search with extra steps
vendeten
vendeten5d ago
I guess yea? Just a thing where a number gets generated and you need to guess it
Angius
Angius5d 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
Core5d ago
Sorry mb, it isn't a binary search. But I don't see the problem
Angius
Angius5d ago
Same, can't see the issue at a glance either
vendeten
vendeten5d ago
Huh
Salman
Salman5d ago
Code works fine for me
vendeten
vendeten5d ago
Weird, asked sir and he said other people were having a similar issue
Salman
Salman5d ago
Just tried
Angius
Angius5d 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
vendeten5d 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
Salman5d ago
I'd just move the random instance creation to the class lever maybe
Angius
Angius5d ago
You mean you get a new number, just like that, without any inputs?
Salman
Salman5d ago
That doesn't happen for me when I try
Angius
Angius5d ago
I'd still debug and see
vendeten
vendeten5d ago
No I input it And then it changes
Angius
Angius5d ago
Place some breakpoints, step through the code Also, show the terminal output when it happens
vendeten
vendeten5d ago
Alright- Maybe i should also just use like visual studio or smth?
Angius
Angius5d ago
I'm curious when exactly it happens Yes
Salman
Salman5d ago
No description
Angius
Angius5d ago
Visual Studio 2022 Community is recommended
Salman
Salman5d ago
What are you using if not VS
Angius
Angius5d ago
If you're using some weird online tepl, that could be it
Core
Core5d ago
My beloved Rider....
Angius
Angius5d ago
Well, yeah, Rider is great, but not free alas Well, free for students
vendeten
vendeten5d ago
Smth the uni advised to use because visual studio could have problems
Angius
Angius5d ago
But
Salman
Salman5d ago
This is programiz online compilers output
Core
Core5d ago
Visual Studio only has problem with the C language, it is perfectly fine for C#
vendeten
vendeten5d ago
Oh my god was it just the compiler or whatever I was using
Angius
Angius5d ago
Could very well be
vendeten
vendeten5d ago
I spent like ages on this At least I know what to use now Thanks everyone
Angius
Angius5d ago
Anytime :Ok:
vendeten
vendeten5d 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
Angius5d 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
MODiX5d 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
Angius5d ago
Not an issue in your case, though
vendeten
vendeten5d 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 User5d ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server