Random Number from 0 to X

Posted on 2006-07-05
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.
Question by:strickdd
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 17

Accepted Solution

rstaveley earned 500 total points
ID: 17043402

Expert Comment

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.



Expert Comment

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.


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 104
Which Linux flavors will this run on? 6 102
What is sub-make ? 2 88
c++  placing data into a form and an editbox 5 25
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

726 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