Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > Random Numbers
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 30th October 14:33
elfour
External User
 
Posts: 1
Default Random Numbers



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


 


2 30th October 14:33
victor bazarov
External User
 
Posts: 1
Default Random Numbers



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
  Reply With Quote
3 30th October 14:34
dirtydog
External User
 
Posts: 1
Default Random Numbers


Can you please type out a full line of code
  Reply With Quote
4 30th October 14:34
w marsh
External User
 
Posts: 1
Default Random Numbers


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?
  Reply With Quote
5 30th October 14:34
david harmon
External User
 
Posts: 1
Default Random Numbers


On 19 Aug 2006 21:06:11 -0700 in comp.lang.c++, "Dirtydog"


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
  Reply With Quote
6 30th October 14:34
robert j. hansen
External User
 
Posts: 1
Default Random Numbers


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).
  Reply With Quote
7 30th October 14:34
greg
External User
 
Posts: 1
Default Random Numbers


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::random_device (assuming it is available for
your compiler) would provide an actual random number.

Greg
  Reply With Quote
8 30th October 14:35
pete becker
External User
 
Posts: 1
Default Random Numbers


If it's available and it doesn't return pseudo-random numbers. It's
allowed to, so check the documentation.
  Reply With Quote
9 30th October 14:35
pete becker
External User
 
Posts: 1
Default Random Numbers


Where in the standard is this requirement? <g>

Many years ago, there were many bad implementations of rand. There still
may be.
  Reply With Quote
10 30th October 14:35
robbie hatley
External User
 
Posts: 1
Default Random Numbers


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<double>(rand()) /
static_cast<double>(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
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Development, Ada, Apple script, Assembler, Awk, Beos, Basic, C, C++, C#, C# .net, .net, .net frameworks, Asp .net, Clarion, Clipper, Clos, Clu, Cobol, Coldfusion, Delphi, Dylan, Eiffel, Forth, Fortran, Haskell, Hermes, Icon, Idl, Java, Java script, Jscript .net, Jcl, Linoleum, Lisp, Lotus, Limbo, Logo, Ml, Mumps, Oberon, Postscript, Pop, Pl1, Prolog, Python, Ruby, Pascal, Perl, Php, Rebol, Rexx, Sed, Sather, Scheme, Smalltalk, Tcl, Vhdl, Vrml, Visual basic, Visual basic .net, Yorick, Mysql, Omnis, Postgresql, Xbase, Access, Oracle, Adabas, Berkeley, Btrieve, Filemaker, Gupta, Db2, Informix, Ingres, Mssql server, Object, Olap, Paradox, Rdb, Revelation, Sybase, Theory, Dbase, Html, Java script, Css, Flash, Photoshop, Corel script, Xml, Tech, Beos, Gem, Hp48, Hpux, Linux, Mac, Ms-dos, Os2, Palm, Solaris, Ti99, Windows, Xenix, Aos, Chorus, Geos, Inferno, Lantastic, Lynx, Mach, Minix, Netware, Os9, Parix, Plan9, Psos, Qnx, Xinu, Sco, Unix, Aix, Aux, 386bsd, Bsdi, Freebsd, Netbsd, Openbsd, Ultrix, Amd, Intel, Aptiva, Buz, Deals, Homebuilt, Overclocking, Programming, Extra forums


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666