For part of a programming that I'm writing, I want to generate random numbers between the range of -1000 and 1000. My teacher never spent much time on the rand function, and I'm not quite sure how to specify a range like that. Any help would be greatly appreciated. Thanks!
rfrapp 17 Junior Poster in Training
Recommended Answers
Jump to PostrandNum = (rand() % range) + firstNum;
range is the total number you want to generate
firstnum is the beginning number of that range
Jump to PostSometimes it helps to see concrete examples:
int r1 = rand() ; //random number from [0 , RAND_MAX] int r2 = rand() % 100; // random number from [0,100); Note doesn't include 100 int r3 = rand() % 100 + 1; //random number from [1,100]; Note includes …
All 6 Replies
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
skorm909 0 Junior Poster
mrnutty 761 Senior Poster
rfrapp 17 Junior Poster in Training
rfrapp 17 Junior Poster in Training
skorm909 0 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.