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