---
title: "Generate Random numbers uniformly over entire range"  
description: "Generate Random numbers uniformly over entire range"  
author: "Anonymous User"  
published: 2013-05-07  
updated: 2013-05-07  
canonical: https://www.mindstick.com/forum/832/generate-random-numbers-uniformly-over-entire-range  
category: "visual c++"  
tags: ["visual c++"]  
reading_time: 1 minute  

---

# Generate Random numbers uniformly over entire range

Hi All!\
I need to [generate random](https://www.mindstick.com/forum/159580/how-to-generate-random-numbers-in-c-sharp) numbers with in specified interval [[max](https://www.mindstick.com/articles/1155/count-max-min-function-in-excel),min]\
Also the random numbers should be uniformly [distributed](https://answers.mindstick.com/blog/43/how-do-you-handle-failures-in-distributed-systems) over interval, not located to particular point\
Currenly I am generating as:\
for(int i=0;i<6;i++){DWORD random= rand()%(max-min+1) + min;}From my [tests](https://yourviews.mindstick.com/audio/1136/sat-subject-tests) random numbers are generated around one point only\
Examplemin= 3604607;max= 7654607;Random numbers generated:\
363159436092933630000362844136363763621404Edit (added from [answers](https://www.mindstick.com/forum/156899/salesforce-marketing-cloud-interview-questions-answers) below): Ok RAND_MAX is 32767. I am on [C++](https://www.mindstick.com/blog/745/array-in-c-plus-plus) [windows](https://www.mindstick.com/articles/311752/how-to-install-and-use-the-google-wifi-software-on-a-windows-or-mac-computer) platform.. Is there any other [method](https://www.mindstick.com/forum/166/webservice-method) to generate random numbers with uniform distribution?\
Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)! \

## Replies

### Reply by AVADHESH PATEL

Hi Tanuj!\
Generally, the high bits show a better distribution than the low bits, so the recommended way to generate [random](https://www.mindstick.com/forum/33419/generating-random-numbers-in-objective-c) numbers of a range for simple purposes is:\
((double) rand() / (RAND_MAX+1)) * (max-min+1) + minNote: make sure RAND_MAX+1 does not overflow! (Thanks Demi)\
the division generates a random number from the interval [0, 1), "stretch" this to the required range. Only when max-min+1 gets close to RAND_MAX you need a "BigRand()" function like posted by Mark Ransom.\
This also avoids some slicing problems due to the modulo, which can worsen your numbers even more.


---

Original Source: https://www.mindstick.com/forum/832/generate-random-numbers-uniformly-over-entire-range

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
