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?? 19 13663
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
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.
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.
>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.
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
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"
On 2 Oct, 07:19, Sanchit <sanchitgupt... @gmail.comwrote :
I have to generate a pseudo random number
rand()
--
Nick Keighley
"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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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
|
by: Sweety |
last post by:
plz reply,
thanks in advance.
bye
|
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....
|
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.
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
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...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |