SurvivorTiger
Programmer
Hi everyone,
I have a problem that seems really simple but I've been having a tough time with it. I want to assign random values in increments of 30, starting at 100, to some "customer" objects let's say. So, customers can have values 100, 130, 160, 190, 220 (assume 220 is the max). Now, I know one solution would be to do something like this:
int myNum = rand() % 5;
if (myNum == 0) {
return 100;
} else if (myNum == 1) {
return 130;
} ... and so on.
But I don't like using "magic numbers" and this just doesn't seem to be the right way...I mean what if my range was from 100 to 10000. Obviously I wouldn't want to have that many if statements. I've tried using enums, but that didn't work exactly like I wanted either...but maybe I was just not doing it right.
Any suggestions please?
I have a problem that seems really simple but I've been having a tough time with it. I want to assign random values in increments of 30, starting at 100, to some "customer" objects let's say. So, customers can have values 100, 130, 160, 190, 220 (assume 220 is the max). Now, I know one solution would be to do something like this:
int myNum = rand() % 5;
if (myNum == 0) {
return 100;
} else if (myNum == 1) {
return 130;
} ... and so on.
But I don't like using "magic numbers" and this just doesn't seem to be the right way...I mean what if my range was from 100 to 10000. Obviously I wouldn't want to have that many if statements. I've tried using enums, but that didn't work exactly like I wanted either...but maybe I was just not doing it right.
Any suggestions please?