get random number in range [10..50]

R

Roman Mashak

Hello,

can you recommend better way to generate random number within the range
[10..50]:

int x;
x = rand() / (RAND_MAX / 50 + 1);
if (x < 10) x = x + 10;

It seems to me clumsy and not efficient.

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
B

boa

* Roman Mashak wrote, On 22.08.2006 08:35:
Hello,

can you recommend better way to generate random number within the range
[10..50]:

int x;
x = rand() / (RAND_MAX / 50 + 1);
if (x < 10) x = x + 10;

It seems to me clumsy and not efficient.

x = (rand() % 40) + 10;
 
R

Roman Mashak

Hello, boa!
You wrote on Tue, 22 Aug 2006 08:55:36 +0100:

b> * Roman Mashak wrote, On 22.08.2006 08:35:
??>> Hello,
??>>
??>> can you recommend better way to generate random number within the
??>> range [10..50]: int x; x = rand() / (RAND_MAX / 50 + 1); if (x < 10) x
??>> = x + 10; It seems to me clumsy and not efficient.
b> x = (rand() % 40) + 10;

FAQ claims this method as very poor.

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
P

pete

boa said:
* Roman Mashak wrote, On 22.08.2006 08:35:
Hello,

can you recommend better way to generate random number within the range
[10..50]:

int x;
x = rand() / (RAND_MAX / 50 + 1);
if (x < 10) x = x + 10;

It seems to me clumsy and not efficient.

x = (rand() % 40) + 10;

.... or

x = rand() % 41 + 10;

depending on just exactly what "within the range [10..50]" means.
 
R

Roman Mashak

Hello, pete!
You wrote on Tue, 22 Aug 2006 08:13:22 GMT:

??>>> can you recommend better way to generate random number within the
??>>> range [10..50]: int x; x = rand() / (RAND_MAX / 50 + 1); if (x < 10)
??>>> x = x + 10; It seems to me clumsy and not efficient.
??>>
??>> x = (rand() % 40) + 10;

p> ... or

p> x = rand() % 41 + 10;

p> depending on just exactly what "within the range [10..50]" means.
I want to get integer values from the range 10 through 50, including 10 &
50.

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
B

boa

* Roman Mashak wrote, On 22.08.2006 09:12:
Hello, boa!
You wrote on Tue, 22 Aug 2006 08:55:36 +0100:

b> * Roman Mashak wrote, On 22.08.2006 08:35:
??>> Hello,
??>>
??>> can you recommend better way to generate random number within the
??>> range [10..50]: int x; x = rand() / (RAND_MAX / 50 + 1); if (x < 10) x
??>> = x + 10; It seems to me clumsy and not efficient.
b> x = (rand() % 40) + 10;

FAQ claims this method as very poor.

The point of my post was just to show you how to scale the numbers to
the proper range. How you generate the random number is up to you.

Boa
 
K

Keith Thompson

Roman Mashak said:
can you recommend better way to generate random number within the range
[10..50]:

int x;
x = rand() / (RAND_MAX / 50 + 1);
if (x < 10) x = x + 10;

It seems to me clumsy and not efficient.

It's also wrong; numbers in the range 10..19 are twice as likely as
numbers above 19.

<http://www.c-faq.com/>, question 13.16.
 
M

Martin Ambuhl

Roman said:
Hello,

can you recommend better way to generate random number within the range
[10..50]:

int x;
x = rand() / (RAND_MAX / 50 + 1);
if (x < 10) x = x + 10;

It seems to me clumsy and not efficient.

Putting a decimal point after the '50' would help you tremendously.

I prefer, by the way,
x = 51 * rand()/(RAND_MAX + 1.);
Note the decimal point after the '1'.
rand()/(RAND_MAX + 1.) will give you a value in [0,1)
multiplying by 51 will give you a value in [0,51)
and the double->int conversion gives you the integers
in the range [0,50], with the probability of 50 as
close as possible to that of each of the integers
in the range [0,49].
 
A

Andrew Poelstra

Roman Mashak said:
Hello, boa!
You wrote on Tue, 22 Aug 2006 08:55:36 +0100:

b> * Roman Mashak wrote, On 22.08.2006 08:35:
??>> Hello,
??>>
??>> can you recommend better way to generate random number within the
??>> range [10..50]: int x; x = rand() / (RAND_MAX / 50 + 1); if (x < 10) x
??>> = x + 10; It seems to me clumsy and not efficient.
b> x = (rand() % 40) + 10;

FAQ claims this method as very poor.

The FAQ also tells you how to do it properly; Q13.16 answers your
original question for you. The group tries to avoid giving perfect
answers so that you will learn rather than copy.

Remember in the future to check the FAQ before posting on the
group, and then ask for help with the FAQ's answer if you need
clarification or a more detailed explanation.
 
S

Simon Biber

Roman said:
Hello,

can you recommend better way to generate random number within the range
[10..50]:

int x;
x = rand() / (RAND_MAX / 50 + 1);
if (x < 10) x = x + 10;

#include <stdlib.h>

int rand_range(int min, int max)
{
return rand() / (RAND_MAX + 1.0) * (max - min + 1) + min;
}

This function generates a pseudo-random number in the range of
[min..max] inclusive.

Here's an example of how to use it:

#include <stdio.h>
#include <time.h>

int main(void)
{
int i, j;
srand(time(NULL));
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
printf("%3d", rand_range(10, 50));
}
putchar('\n');
}
return 0;
}

The output for one example run:

47 28 29 48 20 11 15 48 45 13 49 12 15 38 39 31 40 19 23 29
47 43 35 11 40 20 45 41 49 21 19 18 38 46 18 17 37 11 30 41
18 14 48 39 25 25 20 37 17 17 49 42 39 37 14 12 23 48 28 25
32 40 49 45 12 48 41 14 47 26 29 36 40 12 18 20 21 28 49 21
38 30 14 32 14 19 17 11 40 17 45 40 40 17 28 49 21 16 42 15
31 21 31 48 15 27 26 21 20 33 15 28 11 35 10 39 32 37 24 44
15 11 12 42 33 20 14 48 39 50 30 17 20 34 25 46 17 37 26 44
27 40 42 24 14 50 37 36 23 22 22 26 40 46 15 20 47 24 32 22
11 13 15 13 21 38 37 25 47 39 10 34 49 26 13 31 47 16 27 12
45 28 49 48 47 24 41 50 28 34 41 43 43 30 45 50 14 16 42 50
28 45 24 28 50 10 21 22 13 31 49 41 37 50 25 41 29 50 34 42
26 10 43 25 49 43 33 28 24 16 10 48 48 17 50 31 26 16 27 15
12 32 32 24 37 45 27 31 28 34 11 50 27 15 47 38 35 32 46 36
21 49 22 49 13 23 24 39 34 50 38 41 36 32 29 19 16 17 39 20
25 17 38 25 48 34 50 19 45 49 11 22 12 10 21 38 30 12 16 41
23 24 43 50 33 27 17 50 41 20 20 11 41 19 33 33 30 33 23 22
19 31 27 35 22 23 42 21 29 10 37 18 16 39 36 23 30 10 26 38
22 23 37 33 50 43 49 37 25 45 29 37 10 26 38 12 28 42 14 36
28 44 18 18 29 50 20 12 26 39 30 12 45 25 44 32 40 33 45 28
20 21 38 17 17 30 12 19 38 30 19 47 21 14 44 42 19 30 11 35
 
R

Roman Mashak

Hello, Simon!
You wrote on Tue, 22 Aug 2006 17:34:41 GMT:

[skip]

Thank you for example, I think in function it's important to typecast the
result from double to int:

int rand_range(int min, int max)
{
return (int)(rand() / (RAND_MAX + 1.0) * (max - min + 1) + min);
}

SB> int rand_range(int min, int max)
SB> {
SB> return rand() / (RAND_MAX + 1.0) * (max - min + 1) + min;
SB> }

SB> This function generates a pseudo-random number in the range of
SB> [min..max] inclusive.

SB> Here's an example of how to use it:

SB> #include <stdio.h>
SB> #include <time.h>

SB> int main(void)
SB> {
SB> int i, j;
SB> srand(time(NULL));
SB> for(i = 0; i < 20; i++)
SB> {
SB> for(j = 0; j < 20; j++)
SB> {
SB> printf("%3d", rand_range(10, 50));
SB> }
SB> putchar('\n');
SB> }
SB> return 0;
SB> }

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
F

Frederick Gotham

Roman Mashak posted:
Thank you for example, I think in function it's important to typecast the
result from double to int:

int rand_range(int min, int max)
{
return (int)(rand() / (RAND_MAX + 1.0) * (max - min + 1) + min);
}


That cast serves no other purpose than to suppress a potential compiler
warning -- there is an implicit conversion from double to int in C.
 
I

Ian Collins

Frederick said:
Roman Mashak posted:





That cast serves no other purpose than to suppress a potential compiler
warning -- there is an implicit conversion from double to int in C.
Which warning?

It isn't the result of rand() that is being cast, but the result of the
expression within the parenthesis.

That aside, the cast is superfluous.
 
E

Eric Sosman

Ian said:
Which warning?

"*** warning: possible loss of precision, meathead!"

Maybe your compiler doesn't phrase the warning exactly
this way, or maybe your compiler doesn't issue any warning
at all for "downgrading" conversions. Some do, though --
one once warned me of precision loss in `float f = 0.0;'.
Adding a cast makes your intent explicit, and may persuade
the compiler to keep silence.

Then again, it might also provoke

"*** warning: superfluous cast, meathead!"

.... which illustrates the point that a "Code Shall Be Free
Of All Compiler Warnings" policy must sometimes relent: the
compiler is *always* at liberty to issue diagnostics, even
for "no good reason."

"*** warning: moon in wrong phase, meathead!"
 
W

websnarf

Keith said:
Roman Mashak said:
can you recommend better way to generate random number within the range
[10..50]:

int x;
x = rand() / (RAND_MAX / 50 + 1);
if (x < 10) x = x + 10;

It seems to me clumsy and not efficient.

It's also wrong; numbers in the range 10..19 are twice as likely as
numbers above 19.

<http://www.c-faq.com/>, question 13.16.

A little while ago, the CLC FAQ improved its answer to this question.
(It was previously only recommending numerically unsound solutions.) I
strongly believe it was in response to my webpage on the weakness of
the FAQ. It is still largely incomplete, however. For a much more
correct/serious addressing of this issue, one can read my webpage on
it:

http://www.pobox.com/~qed/random.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top