473,245 Members | 1,817 Online

# 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 16532
* 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
* 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.

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;'.
the compiler to keep silence.

Then again, it might also provoke

.... 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
it:

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

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

Aug 24 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 8 by: Aaron | last post by: I need some help writing this function the function returns a list of random non-duplicate integers in a string, 1 parameter indicating the maximum range. string randGen(int maxRange) ... 11 by: quickcur | last post by: Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number... 70 by: Ben Pfaff | last post by: One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like... 5 by: Peteroid | last post by: I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible... 3 by: JoelPJustice | last post by: I am working through a VBA book by myself to help and try and improve my skills. However, the book does not give you solutions to certain problems. I have worked through this problem up until bullet... 22 by: gagan.singh.arora | last post by: Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C? 13 by: Peter Oliphant | last post by: I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,... 7 by: bipi | last post by: Dear all, I found function rand(), it can create random number but this function can not define the range of number which I want to get it, such as, I want to get random number in the range from... 20 by: Robbie Hatley | last post by: I needed a quick program called "random" that gives a random positive integer from n1 to n2. For example, if I type "random 38, 43", I want the program to print a random member of the set {38,... 2 by: alishaikhji | last post by: I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0... 0 by: abbasky | last post by: ### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE... 0 by: egorbl4 | last post by: Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ... 1 by: davi5007 | last post by: Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the... 0 by: MeoLessi9 | last post by: I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines".... 0 by: DolphinDB | last post by: The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take... 0 by: Aftab Ahmad | last post by: So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =... 0 by: ryjfgjl | last post by: ExcelToDatabase: batch import excel into database automatically... 0 by: marcoviolo | last post by: Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...