468,463 Members | 1,959 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

get random number in range [10..50]

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: mr*@tusur.ru
Aug 22 '06 #1
14 16025
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;

Aug 22 '06 #2
boa wrote:
>
* 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.

--
pete
Aug 22 '06 #3
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.
bx = (rand() % 40) + 10;

FAQ claims this method as very poor.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Aug 22 '06 #4
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;

pdepending 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: mr*@tusur.ru
Aug 22 '06 #5
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.
bx = (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
Aug 22 '06 #6
"Roman Mashak" <mr*@tusur.ruwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 22 '06 #7
Roman Mashak wrote:
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].
Aug 22 '06 #8
"Roman Mashak" <mr*@tusur.ruwrites:
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.
bx = (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.

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 22 '06 #9
Roman Mashak wrote:
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

--
Simon.
Aug 22 '06 #10
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);
}

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

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

SBHere's an example of how to use it:

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

SBint 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: mr*@tusur.ru
Aug 23 '06 #11
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.

--

Frederick Gotham
Aug 23 '06 #12
Frederick Gotham wrote:
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.
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.

--
Ian Collins.
Aug 23 '06 #13
Ian Collins wrote:
Frederick Gotham wrote:
>>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.

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!"

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 23 '06 #14
Keith Thompson wrote:
"Roman Mashak" <mr*@tusur.ruwrites:
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

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Aug 24 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Aaron | last post: by
11 posts views Thread by quickcur | last post: by
70 posts views Thread by Ben Pfaff | last post: by
5 posts views Thread by Peteroid | last post: by
22 posts views Thread by gagan.singh.arora | last post: by
13 posts views Thread by Peter Oliphant | last post: by
7 posts views Thread by bipi | last post: by
20 posts views Thread by Robbie Hatley | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.