473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

random number generation

hi, is this the correct way to generate floating point random numbers >= 0.0
and <= 1.0 that are different each time the program is run?

#include <stdlib.h>
#include <time.h>

srand( (unsigned)time( NULL ) ); //call it only once in a program liftime??
float random_value = rand()/RAND_MAX;
Jul 23 '05 #1
6 1731
Profil1 wrote:
hi, is this the correct way to generate floating point random numbers >= 0.0
and <= 1.0 that are different each time the program is run?

#include <stdlib.h>
#include <time.h>

srand( (unsigned)time( NULL ) ); //call it only once in a program liftime??
float random_value = rand()/RAND_MAX;


I think generation of [pseudo-] random numbers has been covered
extensively in many newsgroups to warrant another discussion on it.
Please use the Web to search the answer to your FAQ.
Jul 23 '05 #2
Profil1 wrote:

hi, is this the correct way to generate floating point random numbers >= 0.0
and <= 1.0 that are different each time the program is run?

#include <stdlib.h>
#include <time.h>

srand( (unsigned)time( NULL ) ); //call it only once in a program liftime??
float random_value = rand()/RAND_MAX;


No

rand() returns an integer
RAND_MAX is an integer

So dividing an integer by another integer gives what?
Knowing that RAND_MAX is the highest value that rand() might
produce, what does this tell us about the outcome of the division.
(eg. if the highest number equals 8, what is the result of
2 / 8
3 / 8
7 / 8
)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3
Profil1 wrote:
hi, is this the correct way to generate floating point random numbers >= 0.0
and <= 1.0 that are different each time the program is run?

#include <stdlib.h>
#include <time.h>

srand( (unsigned)time( NULL ) ); //call it only once in a program liftime?? Yes, it should normally only be necessary to call srand once. However,
the return value of time() will only change once per second, so if you
start two instances of the program within one second, they will have the
same sequence of random numbers.
float random_value = rand()/RAND_MAX;

To be sure that you actually get a float, and not just something rounded
to 0 or 1, you could write

float random_value = static_cast<float>(rand())/RAND_MAX;

instead.

/ martin
Jul 23 '05 #4

Martin Magnusson wrote:
float random_value = static_cast<float>(rand())/RAND_MAX;


Also consider
const float inv_RAND_MAX = 1.0 / RAND_MAX;
float random_value = rand()*inv_RAND_MAX;

Might be more efficient, multiplication is usually cheaper and
(almost?) never more expensive. I think it's not a legal
optimalization for an IEEE-conforming compiler as the results
might differ a bit, but in this case you don't care about it.

Regards,
Michiel Salters

Jul 23 '05 #5
Martin Magnusson wrote:
Yes, it should normally only be necessary to call srand once. However,
the return value of time() will only change once per second, so if you
start two instances of the program within one second, they will have the
same sequence of random numbers.


If you want to use a higher resolution clock for the random seed, you
can do like this

#include <stdlib.h>
#include <sys/time.h>
#include <iostream>
using namespace std;

int main()
{
struct timeval time;
gettimeofday( &time, 0 );
srand( time.tv_usec );
float random_value = static_cast<float>(rand())/RAND_MAX;
cout << random_value << "\n";
}
/ martin
Jul 23 '05 #6
In article <1112600410.38806c8dd45da8a3a4a37b6ab0a2ed61@teran ews>,
Martin Magnusson <martin@-xx-blecket-xx-.org> wrote:
Profil1 wrote:
float random_value = rand()/RAND_MAX;

To be sure that you actually get a float, and not just something rounded
to 0 or 1, you could write

float random_value = static_cast<float>(rand())/RAND_MAX;


While this is common advice, and often works well in practice, this
can cause serious numerical biases. For instance, if RAND_MAX=65536
and you're selecting from 100,000 elements at random, you'll never
select some of the elements.

boost has done quite a bit to make these issues. See:
<http://www.boost.org/libs/random/>
--
Mark Ping
em****@soda.CSUA.Berkeley.EDU
Jul 23 '05 #7

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

Similar topics

10
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...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
10
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of...
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
4
by: Dimos | last post by:
Hello All, I need some help with random number generation. What I need exactly is: To create a few thousand numbers, decimal and integers, between 5 and 90, and then to export them as a...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
16
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.