I built a rock paper scissors game but it always returns a draw. can anyone help me?
providing code here👇
77 Replies
you already have an open forum thream
why open another one?
i closed it
i wouldn't open a new one w/o closing the last one
it's definitely still open
i can see it
it says "open post"
100% still there and active.
i closed it tho
no, you deleted the initial message.
thats not the same thing
also the post
and also, why did you do that?
you were given a lot of useful help in that thread.
there is no reason to open a new thread and ask teh same question.
it says close post right there
then new ppl will see it bc the last person had to go
Thats not how forum threads work
that's not how it works man
oh i reopened it
you still had the 3rd most recent post
"bumping" by re-posting just removes context
oh ok so do i close this one and open the last one?
whatever just leave it like this now
it's because you instantiate a new
Random
both timesbut its a different random
code too fast -> same random seed -> same random result
^
what
when you do
new Random()
it takes the seed from the computer clockthen how do i change it?
use the same random instance
upgrade to .net 6 and use
Random.Shared
i haven't learned it yet
?
haven't learned what?
you don't even need
RockPaperScissorsOne
and RockPaperScissorsTwo
. you can just have a singular RockPaperScissors
. there's no need for two of them
Random.Shared is the better way, but this shows how to re-use a random instance
where do i put it in the code
You don't just copy paste it, you read it
wdym
I mean exactly that
Read the code, try to understand what it does
and implement it in your code
what is ArgumentOutOfRangeException()
in this case, not really important
since
.Next(0,3)
will never return anything else than 0, 1 or 2i need to put it in internal class tho
Okay.
Then do so.
i dont get, can you please help me by using things i know?
i changed it a bit but the only time it doesnt give me draw it gives me one paper one scissors
same issue still
you're creating a new
Random
instance two times in a row, they both have the same seed (because the seed is based on the system time or something?), meaning they both get the same resulthow do i use another random?
sometimes they dont tho
pobiega gave you a version here
I haven't learned these things yet. I must use things i know bc its for school
is there another version?
what are "these things"?
switch,throw new, ArgumentOutOfRangeException()
instead of switch i used if
sure, you can do that
Also the changes you made in the recent version don't make sense
you've made choice 1 be rock 1/3 of the time and scissors 2/3 of the time
and then
you made the choice2 graphic depend on choice1
i changed it after
the scissors i changed it into paper
but i opened a new random doesnt it supossed to change?
no
From https://learn.microsoft.com/en-us/dotnet/api/system.random?view=net-7.0
On most Windows systems, Random objects created within 15 milliseconds of one another are likely to have identical seed values.
Random Class (System)
Represents a pseudo-random number generator, which is an algorithm that produces a sequence of numbers that meet certain statistical requirements for randomness.
is there another way to write random? so it wont have the same result
yes, like this
but its still using the same random
that's the whole point
yes Pobiega already showed you - you create a field in RPS that is Random object and then use that field to generate your random numbers
you're supposed to use the same random
what you're currently doing is create a new random every call
you just said you can replace switch with if-else
do that
we're not here to babysit you, this is incredibly basic
don't expect code that you just copy and paste to work
(also you're on an outdated .net version, count yourself lucky to get help at all)
i learned this a week ago ofc* its basic
think of Random like a die and everytime you call Random.Next, you're rolling the die and getting a result
if you create two Random instances within 15 milliseconds of each other, you're creating the exact same die twice and so when you roll the dice, you get the same results on each die.
However if you roll one die twice instead, you'll get different results. thats why you want to use 1 die rather than creating two dice set on the same seed
thx, i get the idea of it but im just not sure on how to code it
this is suppossed to work?
considering that's just your original code, no
i just dont see whats wrong
i know that the seed is the same but i used two randoms so i thought its supposed to work
no, that's the exact reason it doesn't work
so what do i do?
2 randoms with the same seed are obviously going to give the same results
you do this
???
i dont understand
do i use for?
bc i want to use it twice?
just to piggyback off this to get a better understanding of what I'm reading from the docs, the issue here is that if you have two calls to
GetChoice
simultaneously, one is going to return 0
but if you used Random.Shared, you won't have a problem?
Guessing this also wouldn't be an issue if the program was single-threaded?so i cant have one function for each choice?
you shouldn't have one for each choice
both methods literally do the same thing
they have the same exact content
in your
main
you want to call GetChoice
to get a random value for choice1
and then call it again for choice2
how do i do that(sorry im just new to this)
It just does it once and returns it twice