rand() random numbers between 1-10 but not allow specific numbers

Hello together,

i know how to use rand() but is it possible to generate random numbers from 1-10 but dont allow number 9?
Yep.

1
2
3
4
int random_number;
do { 
   random_number = 1 + rand() % 10 
} while (random_number == 9);


You might want to use the random generator that comes with the <random> header (you don't have to, but a lot of people insist).

1
2
3
4
5
6
7
8
9
10
unsigned short int get_random_number(const unsigned short int lower_limit, const unsigned short int higher_limit) { 
// limits are both inclusive
	static std::random_device random_number_generator_seed;
	static std::mt19937 random_number_generator(random_number_generator_seed());
	static std::uniform_int_distribution<unsigned int> random_interval;

	random_interval.param(std::uniform_int_distribution<unsigned int>::param_type(lower_limit, higher_limit));
	
	return random_interval(random_number_generator_seed);
}
Don't forget to #include <random> should you use this function.
@Grime, that's overly complicated.

Just generate a number from 1 to 9, and if it's 9, add 1.

1
2
int r = rand() % 9 + 1;
if (r == 9) ++r;

Last edited on
Of course.
1
2
3
4
int n;
do
    n = rand();
while (n < 1 || n > 10 || n == 9);
dutch perhaps you meant rand () % 10 + 1, intelligent ;']
Last edited on
Grime wrote:
dutch perhaps you meant rand () % 10 + 1, intelligent ;']

Pardon?

@dutch's code is quick, wastes no rand() calls and above all ... is correct.

This would also do:
1
2
int r = rand() % 9;
if ( r == 0 ) r = 10;
Last edited on
Grime wrote:
dutch perhaps you meant rand () % 10 + 1, intelligent ;']

I've noticed that you aren't that bright, but this is a new low for you.

As another example, suppose you wanted values from 1 to 10 but skipping 2, 5, and 8:

1
2
3
4
int r = rand() % 7 + 1;
if (r >= 2) ++r;
if (r >= 5) ++r;
if (r >= 8) ++r;

Last edited on
I don't know what Grime's second post is implying, but the issue is the if (r == 9) ++ r; implementation makes the distribution significantly non-uniform (well, more non-uniform than just using % 10 to begin with).

(I misread the post, please just ignore this post.)

Now, maybe that doesn't matter for Chris26's purposes.
There might be a better way to implement whatever Chris is trying to do, such as changing the logic a bit to allow shuffling.
Last edited on
some sequences all I know to do is prepopulate a vector and get a random *index* into the list.
this isnt one of those problems, but say you needed to choose a random value from {1,7,13,18,32,100} ... its easier to just get a random value from 0 to 5 and pick off the value from your list. Just in case you run into this kind of problem again.
Yes, if the pool of possible numbers is small enough, producing a uniform random index into an arbitrary list would be preferred. But if the pool of numbers is absurdly large, then either the loop or making the distribution slightly non-uniform would be preferred.
Last edited on
but the issue is the if (r == 9) ++ r; implementation makes the distribution

I disagree. You are choosing from 9 numbers. That's how I interpret it.
Huh? What are you disagreeing with?
the issue is the if (r == 9) ++ r; implementation makes the distribution significantly non-uniform (well, more non-uniform than just using % 10 to begin with).
No, it doesn't. If you have a generator that selects an element from the set {1, 2, 3, 4, 5, 6, 7, 8, 9}, adding if (r == 9) ++r; after the generation transforms the selection set to {1, 2, 3, 4, 5, 6, 7, 8, 10} without altering the uniformity for elements 1-8 and moves the uniformity of 9 to 10. In other words,
P'(1) = P(1)
P'(2) = P(2)
P'(3) = P(3)
P'(4) = P(4)
P'(5) = P(5)
P'(6) = P(6)
P'(7) = P(7)
P'(8) = P(8)
P'(10) = P(9)
Oooooh, I see what you guys are saying now. Yeah my bad. I misread it as
1
2
int r = rand() % 10 + 1;
if (r == 9) ++r;
I think I accidentally read it like that because my mind was combining dutch's and Grime's post.

I'll be more careful next time.
Last edited on
dutch wrote:
I've noticed that you aren't that bright, but this is a new low for you.

You just had to say "no". Two letters, would take you what 250 milliseconds max?
I can see that you're perfect and never misread anything, and for that I congratulate you.
Last edited on
Grime wrote:
I can see that you're perfect and never misread anything, and for that I congratulate you

I've noticed you saying a number of stupid things. You are an idiot. That's what I was trying to say. I hope that's clear to you now. Moron. :-)
dutch wrote:
I've noticed you saying a number of stupid things. You are an idiot. That's what I was trying to say. I hope that's clear to you now. Moron. :-)

Surely insulting other people isn't a "stupid" thing to do. Anyways you be you and I'll be me. At the end of the day you're some stranger and I'm some stranger.
Topic archived. No new replies allowed.