Solved

Random Number from 0 to X

Posted on 2006-07-05
3
225 Views
Last Modified: 2010-04-01
I need a random number generator that will give me a different number each time it is executed. The purpose of this is to insert random FLOAT numbers into an array.
0
Comment
Question by:strickdd
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 500 total points
ID: 17043402
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17043526
Hi there,

//** a = [0...1[  
double a = (double)(rand()%RAND_MAX) / RAND_MAX

will generate pseudo random floating point values between 0 and 0.999...
There are better random number generators than the standard c implentation.

Get back if you have higher demands on the randomness.

Cheers,
Sebastian

0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17043554
one addition:

The rand() function wll provide the same sequence of random integer values [0...RANDMAX] whenever your application restarts.
Use srand((unisnged int) time());
to intialize ('seed') the random number sequence.


Cheers,
Sebastian
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now