You must not create multiple new instances of Random
in that way. The documentation says:
The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. This problem can be avoided by using a single Random object to generate all random numbers.
This means that at the start of StartHand
you typically have k == j
and hence make the recursive call. In turn this leads to the stack overflow.
Create exactly one instance of the random number generator. You might like to use a better seed than the system clock. All random numbers should come from this single instance of Random
.
You would be well advised to use a loop rather than recursion as a means to ensure that k
differs from j
.
Your attempts to score are completely wrong. You appear to attempt that in randomcard
. But you only score into a local variable whose value is not remembered. I think you meant for the score parameter of randomcard
to be an out
parameter.
I would suggest that you need to adopt a more functional style of coding. Have a function that draws a card. Have the card be represented by a struct with suit and value members. Have a function that returns score given a value. Stop using so many static members and use locals and return values where possible.