473,545 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random Numbers

In c++ what is the code to make teh program randomly select a number?

Aug 20 '06 #1
28 2552
El****@gmail.co m wrote:
In c++ what is the code to make teh program randomly select a number?
Usually we employ the help of 'rand' function from the C library.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 20 '06 #2

Victor Bazarov wrote:
El****@gmail.co m wrote:
In c++ what is the code to make teh program randomly select a number?

Usually we employ the help of 'rand' function from the C library.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Can you please type out a full line of code

Aug 20 '06 #3
Dirtydog wrote:
Victor Bazarov wrote:
>El****@gmail.co m wrote:
>>In c++ what is the code to make teh program randomly select a number?
Usually we employ the help of 'rand' function from the C library.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Can you please type out a full line of code
Please make the effort to learn the language well enough to call a
simple function when you have been given its name. Anybody would think
that Google cost $10 per search by the way you people go on. Are you
really so mentally dull that you can't show any initiative?
Aug 20 '06 #4
On 19 Aug 2006 21:06:11 -0700 in comp.lang.c++, "Dirtydog"
<El****@gmail.c omwrote,
>
Can you please type out a full line of code
The usual answer to that is the same in C++ as it is in C, and is
covered in Steve Summit's C FAQ. It is always good to check the FAQ
before posting. You can get the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html
Aug 20 '06 #5
In c++ what is the code to make teh program randomly select a number?

Effectively, there isn't. rand() has a lot of very bad, bad
properties. It's generally useless for cryptography, for Monte Carlo
simulations, for randomized algorithms, for... etcetera.

If you know that you only need very low-quality random numbers, use
rand(). If you need anything better quality, use your operating
system's built-in facilities (usually CryptGenRandom or /dev/urandom).

Aug 20 '06 #6

Victor Bazarov wrote:
El****@gmail.co m wrote:
In c++ what is the code to make teh program randomly select a number?

Usually we employ the help of 'rand' function from the C library.
But rand() returns a "pseudo-random" number. Each number returned
depends on the previous one. The entire sequence that rand() returns
only appears to be random. In reality the series is deterministic and
will start to repeat itself after rand() has been called enough times.

A truly random number would have to obtained in a non-deterministic
way. In that case std::tr1::rando m_device (assuming it is available for
your compiler) would provide an actual random number.

Greg

Aug 20 '06 #7
Greg wrote:
>
A truly random number would have to obtained in a non-deterministic
way. In that case std::tr1::rando m_device (assuming it is available for
your compiler) would provide an actual random number.
If it's available and it doesn't return pseudo-random numbers. It's
allowed to, so check the documentation.
Aug 20 '06 #8
Robert J. Hansen wrote:
>>In c++ what is the code to make teh program randomly select a number?


Effectively, there isn't. rand() has a lot of very bad, bad
properties.
Where in the standard is this requirement? <g>

Many years ago, there were many bad implementations of rand. There still
may be.
Aug 20 '06 #9

<El****@gmail.c omwrote:
In c++ what is the code to make the program randomly select a number?
Here's some functions I wrote a while back to get decent pseudo-random
integers and doubles:

First, to seed the random number generator by time (which makes the
pseudo-random numbers less repeatable, and hence less "pseudo"), run
THIS function *ONCE ONLY* at the beginning of your main():

#include <cmath>
inline void Randomize(void)
{
srand(time(0));
}

Now a function to get a pseudo-random double within a given range:

// Get random double:
inline double RandNum(double min, double max)
{
return min + (max - min) * (
static_cast<dou ble>(rand())
/
static_cast<dou ble>(RAND_MAX)
);
}

And finally, a function to get a pseudo-random integer within a given range:

// Get random int:
inline int RandInt(int min, int max)
{
return static_cast<int >(RandNum(min + 0.001, max + 0.999));
}

If you're wondering about the 0.001 and 0.999, they're necessary to prevent
two different kinds of error:

1. Fencepost errors. If you don't carfully skew the end points, the minimum
and maximum integers of your range will have dramatically different
probability of occurring than any of the other integers in the range.
For example, to get equal probabilities for the interval [5,9], you
need to feed min=5.001, max=9.999 to RandNum. Thus the range of
doubles corresponding to 9 is about 1. (If you had fed the more
intuitive value of 9.000 to RandNum, then 9's probability would
be 0.000%, not 20.000% as it should be.)

2. Roundoff errors. When casting back and forth between double and int,
it's easy to accidentally end up with a number that's 1 less or more
than it should be. Hence I add a saftey margin of 0.001.

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
home dot pac bell dot net slant earnur slant
Aug 20 '06 #10

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

Similar topics

10
11932
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was using to get random numbers was Random rn = new Random(System.currentTimeMillis()); but it seems that the system doesn't update the milliseconds...
3
7360
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I have utilised the following random number source code http://www.agner.org/random/ What I have found is that there is a problem with seeding. The...
21
2994
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX. Thank you for your help
5
2386
by: cvnweb | last post by:
I am trying to generate 2 random numbers that are diffrent, in order to add them to existing numbers to generate numbers that start out the same, but are randomly added and subtracted so that they can go down similar paths, but not be the same. I will implement code later to make sure i they go more than 10 apart from each other that they are...
104
5059
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random...
12
5202
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
21
13488
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
13
2776
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, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: ...
6
11732
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program that will randomly generate 10 unique random numbers. Your job is to write a program that produces random permutations of the numbers 1 to 10....
24
7183
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me uniformly distributed and unique numbers ?
0
7468
marktang
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...
0
7401
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...
0
7808
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...
1
7423
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...
0
7757
tracyyun
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...
0
5972
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...
1
5329
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...
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.