Here’s how
double fRand(double fMin, double fMax) { double f = (double)rand() / RAND_MAX; return fMin + f * (fMax - fMin); }
Remember to call srand() with a proper seed each time your program starts.
[Edit] This answer is obsolete since C++ got it’s native non-C based random library (see Alessandro Jacopsons answer) But, this still applies to C
Related Posts:
- How to generate a random number in C++?
- rand() between 0 and 1
- Random number between 1 to 10 using C++
- srand(time(0)) and random number generation
- Random float number generation
- Random number c++ in some range
- C++, random number w/ range of 1-6
- Generate random float between two floats
- How do I create a random alpha-numeric string in C++?
- C++ Simple Dice roll – how to return multiple different random numbers [duplicate]
- Weighted random numbers
- What is the effect of extern “C” in C++?
- What exactly is the difference between “pass by reference” in C and in C++?
- What is `CString`?
- Difference between != and =! with an example(in C++)
- Unresolved external symbol in object files
- How do sizeof(arr) / sizeof(arr[0]) work?
- how to implement Interfaces in C++?
- What exactly is nullptr?
- How to make a SIMPLE C++ Makefile
- finding dll for “The specified module could not be found”
- no matching function for call to ‘ ‘
- 1e-9 or -1e9, which one is correct?
- Returning an empty string : efficient way in c++
- invalid conversion from ‘const char*’ to ‘char*’
- Evaluate a string with a switch in C++ [duplicate]
- Expected initializer before function name
- Inheriting constructors
- Pass a vector by reference C++
- lvalue required as left operand of assignment – Array
- What is uintptr_t data type
- Why do we use volatile keyword? [duplicate]
- C++ round a double up to 2 decimal places
- Windows 7 exception code: 0xc0000409
- Convert float to std::string in C++
- “items list” or “item list”
- When and why do I need to use cin.ignore() in C++?
- What does int & mean
- Evaluate a string with a switch in C++ [duplicate]
- “&” meaning after variable type
- undefined reference to ‘std::cout’
- warning: control may reach end of non-void function
- Error at constructor : Expected an identifier?
- identifier “string” undefined?
- What does LPCWSTR stand for and how should it be handled with?
- A warning – comparison between signed and unsigned integer expressions
- How can I convert const char* to string and then back to char*?
- VC++ fatal error LNK1168: cannot open filename.exe for writing
- Reading integers from file and store them in array C++ [closed]
- Uninitialised value was created by a stack allocation
- C++ Image Processing Libraries
- Printing the correct number of decimal points with cout
- error C2011: ” : ‘class’ type redefinition
- error: request for member ‘..’ in ‘..’ which is of non-class type
- Socket API or library for C++?
- Initialize empty vector in structure – c++
- C++ catching all exceptions
- error: new types may not be defined in a return type
- error: passing ‘const …’ as ‘this’ argument of ‘…’ discards qualifiers
- Printing an array in C++?
- C++ “Access violation reading location” Error
- Getting error: ISO C++ forbids declaration of with no type
- IntelliSense: the object has type qualifiers that are not compatible with the member function
- codingbat-like site for C++
- extra qualification error in C++
- RPN Calculator for C++
- How can I get the list of files in a directory using C or C++?
- How do you make a HTTP request with C++?
- Error: C2228: left of ” must have class/struct/union
- error C2679: binary ‘<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
- Multi-character constant warnings
- How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?
- How do I get the type of a variable?
- What is the meaning of a C++ Wrapper Class?
- terminate called after throwing an instance of ‘std::out_of_range’ what(): basic_string::substr
- error C2106: ‘=’ : left operand must be l-value
- Does C++11 have C#-style properties?
- How to print a string in C++
- How to check if input is numeric in C++
- C++ Big Integer
- Learning to work with audio in C++
- In c++ what does a tilde “~” before a function name signify?
- C++ Void Function with File Stream Error
- sorting in std::map where key is a std::string
- System not declared in scope?
- no debugging symbols found when using gdb
- C++ Array of pointers: delete or delete []?
- What is the best way to develop a C++ web application?
- How to install Visual Studio Build Tools 2010 on Visual Studio 2015 Community?
- Throwing out of range exception in C++
- Is null reference possible?
- Negative RGB Values
- DEV C ++ Error: expected declaration before ‘}’ token
- “cannot be used as a function error”
- error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
- Displaying contents of a vector container in C++
- Converting string to unsigned int returns the wrong result
- Link error “undefined reference to `__gxx_personality_v0′” and g++ [duplicate]
- How to use bitmask?
- -Error reading characters of string