473,599 Members | 3,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
extensive discussion and references in this thread not answer your
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
news:39******** *************** ***********@n38 g2000prl.google groups.com...
>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
news:39******** *************** ***********@n38 g2000prl.google groups.com...
>>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.

Similar topics

2
2149
by: vishal | last post by:
hi i want to generate a string which contains 5 characters and all 5 characters are randomly generated means it is not fixed that which string i will get after i execute function. so tell me random number generation in php thxs in advance.
3
6236
by: vishal | last post by:
i want to generate a random number of a fixed length so how can i do this ?? i know some function which returns a single random character at a time but is there any built-in function which takes length of string and return a random string of that length?????? can i implement mechanism so that the random number generated is form a list of characters i specify. e.g. suppose if i want to create a random
12
5006
by: Sweety | last post by:
plz reply, thanks in advance. bye
4
10558
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he guessed right, it asks the user is he/she wants to play again. If the answer is yes, it generates a random number and asks the user to guess the number again. The user can exit if he enters 0. I have created the following code so far but it does not work....
2
4060
by: Man4ish | last post by:
How to generate random number in normal distribution without using any external library. I found few methods implementing it. But all use GSL or boost. I don't want to use any third party library. Thanks in advance.
0
8398
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8400
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8051
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5850
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3898
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3940
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2414
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1505
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.