0

I'm trying to divide a random number that is generated by 13. It can range from 1, 53. I need to divide the random number 13 to get a whole number (to determine what suit it is in a deck of cards). I need the suit to be out of 1-4.

Random number:

value = MyRandom.Next(1, 53);

Division:

suit = value / 13;
face = value % 13;

The suit keeps generating a 0 by the way.

user3158491
  • 45
  • 1
  • 1
  • 5

3 Answers3

2

I'm going to take a stab in the dark as to what you're really asking.

I'm guessing you're actually getting non-zero values for suit, but you're also occasionally getting zero. The main issue here, in this case, ultimately boils down to 0-based vs 1-based indexing.

What you need to do is do all the generating/computing with 0-based indexing, and then add 1 to shift to 1-based indexing. (Alternatively, you could use 0-based indexing)

Example code:

value = MyRandom.Next(0, 52); // notice the values are inclusively between 0 and 51
suit = (value / 13) + 1; // this will be between 1 and 4
face = (value % 13) + 1; // this will be between 1 and 13
Drew McGowen
  • 11,471
  • 1
  • 31
  • 57
  • This is very similar what I was trying to say. I must have failed horribly, since I've managed to garner -2 for a very similar answer. Anyway, +1 since it's the correct answer. – Beska Feb 06 '14 at 03:33
-1

To accomplish the task you are interested in, you should use a combination of casting your value to double and performing the Math.Ceiling operator.

Your Suit code should be written as:

var suit = Math.Ceiling((double)value / 13);
Claies
  • 22,124
  • 4
  • 53
  • 77
-1

It would be helpful if you posted a little more code. Random is not an easy thing for a computer to do because so much architecture goes into not doing random calculations. Therefore, the random generator in .net is a pseudorandom number sequence based off of a seed value. You can come up with your seed however you wish, but I've often seen the ticks of the current DateTime used. Here is an example using your problem and I got the expected outcome that you are looking for:

var rand = new Random((int)(DateTime.Now.Ticks % int.MaxValue));
var value = rand.Next(1, 53);
var suit = value / 13;
var face = value % 13;

Hope this helps. Good luck!

ohiodoug
  • 1,493
  • 1
  • 9
  • 12
  • The default constructor for Random uses the system clock to derive its seed, so seeding it yourself isn't usually necessary. The only reason you'd need to that I can think of is if you have to instantiate multiple ones in rapid sequence, which isn't the best practice on its own. – Curtis Rutland Feb 06 '14 at 03:52
  • My bigger concern was that he was was instantiating a new Random each time through a loop and seeding with the same number. Since he didn't post code showing how he's creating his Random object, we won't know. Integer division and the mod operator should work regardless if his numbers are shifted by 1. Good catch on the default constructor. – ohiodoug Feb 06 '14 at 03:57
  • Minor: Does not `% int.MaxValue` incorrectly prevent `Random()` from ever getting a seed of `int.MaxValue`? – chux - Reinstate Monica Feb 06 '14 at 18:28