473,599 Members | 3,091 Online

# How to generate random number between two numbers

I want to generate a randowm number between two numbers x and y i.e

int randomNoBetween (int x, int y);

Plz help
Is there any such function??
Sep 29 '08 #1
19 13659
Sanchit wrote:
I want to generate a randowm number between two numbers x and y i.e

int randomNoBetween (int x, int y);
The (I guess) simplest way to get a random number in the interval [0, n)
(that is, 0 inclusive, n exclusive) is (if n is small enough) probably
(rand() % n), which will however slightly favor smaller numbers.

I can't tell if there's any "consensus" on whether to use this method or
some more expensive (like floating point arithmetic) to do this.

With this method, it is trivial to write your function in question; this
is left as an exercise to you ;)

Daniel

--
Done: Arc-Bar-Cav-Rog-Sam-Val-Wiz
To go: Hea-Kni-Mon-Pri-Ran-Tou
Sep 29 '08 #2
On Sep 29, 2:43 pm, Daniel Kraft <d...@domob.euw rote:
Sanchit wrote:
I want to generate a randowm number between two numbers x and y i.e
int randomNoBetween (int x, int y);
See question 13.16 of the C-FAQ.
<http://c-faq.com/>
The (I guess) simplest way to get a random number in the interval [0, n)
(that is, 0 inclusive, n exclusive) is (if n is small enough) probably
(rand() % n), which will however slightly favor smaller numbers.
Yes, see question 13.18 of the aforementioned FAQ to learn why.
Sep 29 '08 #3
On Sep 29, 7:43*am, Daniel Kraft <d...@domob.euw rote:
Sanchit wrote:
I want to generate a randowm number between two numbers x and y i.e
int randomNoBetween (int x, int y);

The (I guess) simplest way to get a random number in the interval [0, n)
(that is, 0 inclusive, n exclusive) is (if n is small enough) probably
(rand() % n), which will however slightly favor smaller numbers.
You can eliminate the bias by computing the largest multiple of
RAND_MAX that's evenly divisible by n. Call this M. Then just throw
away random numbers until you get one in the range 0 to M-1. Call
this R. Then return R % n.

Sep 29 '08 #4
>I want to generate a randowm number between two numbers x and y i.e

Generating random numbers requires specialized hardware. Do you
want pseudo-random numbers instead? There's a big difference,
especially to those using cryptography.
>
int randomNoBetween (int x, int y);

Plz help
Is there any such function??
Generally, you can take whatever pseudo-random number generator you
have that generates numbers in a range you can't control, and use
algebra to transform that range into the one you want. Sometimes
this will result in inaccurate probability distribution.

Sep 29 '08 #5
On Sep 29, 11:36*pm, gor...@hammy.bu rditt.org (Gordon Burditt) wrote:
I want to generate a randowm number between two numbers x and y i.e

Generating random numbers requires specialized hardware. *Do you
want pseudo-random numbers instead? *There's a big difference,
especially to those using cryptography.
int randomNoBetween (int x, int y);
Plz help
Is there any such function??

Generally, you can take whatever pseudo-random number generator you
have that generates numbers in a range you can't control, and use
algebra to transform that range into the one you want. *Sometimes
this will result in inaccurate probability distribution.
I have to generate a pseudo random number
Oct 2 '08 #6
Sanchit <sa************ @gmail.comwrite s:
On Sep 29, 11:36*pm, gor...@hammy.bu rditt.org (Gordon "rude" Burditt) wrote:
[...]
>Generally, you can take whatever pseudo-random number generator you
have that generates numbers in a range you can't control, and use
algebra to transform that range into the one you want. *Sometimes
this will result in inaccurate probability distribution.

I have to generate a pseudo random number
You *have* to? Why?

Did you have a question beyond what you asked originally? Did the
question?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 2 '08 #7
On 2 Oct, 07:19, Sanchit <sanchitgupt... @gmail.comwrote :
I have to generate a pseudo random number
rand()

--
Nick Keighley
Oct 2 '08 #8

"Sanchit" <sa************ @gmail.comwrote in message
>I want to generate a randowm number between two numbers x and y i.e

int randomNoBetween (int x, int y);
Given a function random() which generates a (necessarily) pseudo-random
integer with enough bits for your purpose:

/* Return random number from x to y inclusive */
int randomNoBetween (int x, int y) {
return (random() %(y-x+1))+x;
}

You will have to find your own random(); I just use something like this:

#include <stdlib.h>

int random() {
return (rand()<<15) | rand();
}

--
Bartc

Oct 3 '08 #9
"Bartc" <bc@freeuk.comw rites:
"Sanchit" <sa************ @gmail.comwrote in message
>>I want to generate a randowm number between two numbers x and y i.e

int randomNoBetween (int x, int y);

Given a function random() which generates a (necessarily)
pseudo-random integer with enough bits for your purpose:

/* Return random number from x to y inclusive */
int randomNoBetween (int x, int y) {
return (random() %(y-x+1))+x;
}

You will have to find your own random(); I just use something like this:

#include <stdlib.h>

int random() {
return (rand()<<15) | rand();
}
Eek! The general advice is to leave a PRNG alone unless you know it
has a fault and your fix is correct for it. I've seen more problems
caused by "fixed" PRNGs than by intrinsically bad ones.

The above is not a good idea. It has probable undefined behaviour (on
most systems) and will produce badly biased results if RAND_MAX is not
65535.

--
Ben.
Oct 3 '08 #10

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