Solved

Random Number from 0 to X

Posted on 2006-07-05
3
247 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

832 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