From: hgfernan AT usp DOT br Subject: Re: about pseudo-random numbers (NBY) 20 Jan 1998 02:57:29 -0800 Message-ID: <9801181404.AA68296.cygnus.gnu-win32@spider.uspnet.usp.br> References: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7BIT To: Valter EOR Santos , GNU-Win32 Mailing List Cc: GNU-Win32 Mailing List On 13 Jan 98 at 11:50, Valter EOR Santos wrote: > Hi! > I am a newby in this mailing list... > I am generate a program code that need random number generation... > My question is if there is any way to make a limit in the random > generation. In the Turbo Borland C++ i have the unit random(int limit) > that produces numbers between 0 and limit. In the gnu, or other linux > compiler i only have the rand() or srand() units that produces numbers > between 0 and a pre-defined random_limit variable MAX_RAND (or something > like that). How i can generate code for resolving my problem??? > Hello Valter! #include /* MAX_RAND, srand(), rand() */ double random (const int limit) { return (((double)rand () / RAND_MAX) * limit); } The first part of the return expression -- ((double)rand () / RAND_MAX) -- generates a pseudo-random number, converts it to a double value, then divides it by the maximum of pseudo-random function rand(). This yields a floating point number between 0 and 1,0 , with lots of fractional values in between. If rand() wasn't converted to a double, the division would yield only 0 or 1. Next, the result of the division is multiplied by limit to yield the numbers you're looking for, something between zero and limit. These numbers will be fractionary. If you really need them integer, all you gotta do is change the random() function header to int random (const int limit) The function srand() sets a seed, or a starting value for the sequence of pseudo-random numbers. If you run your program twice with the same seed, you end up with exactly the same number sequence. That's what the "pseudo" means. The rand() function, and so our newly defined random(), create numbers that appear to be random. In our case, the statistical properties of the rand() function won't be changed, since in mathematical terms what we're doing is a simple multiplication by (limit / RAND_MAX). People usually set it to a huge number that changes quickly. Yes, you got it: the time in seconds. In the Cygwin environment, you'd better use the GetTickCount() function. So, somewhere in your program you can have this: #include /* MAX_RAND, srand(), rand() */ #include /* GetTickCount() */ .... srand (GetTickCount ()); /* set seed */ .... val = random (limit); /* get a (pseudo) random number */ .... HTH Regards, ++Hilton P.S.: If you still need information, write privately to me: i can send you references to papers in professional magazines that can help you. ------------------- Hilton Fernandes email: hgfernan AT usp DOT br www: http://www.lsi.usp.br/~hilton.html (inactive) M. Sc. Student of Parallel Distributed Applications at Escola Politecnica (Polytechic School) University of S. Paulo - Brazil - For help on using this list (especially unsubscribing), send a message to "gnu-win32-request AT cygnus DOT com" with one line of text: "help".