954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Random numbers between -1000 and 1000

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
Junior Poster in Training
57 posts since Dec 2011
Reputation Points: 27
Solved Threads: 0
 

randNum = (rand() % range) + firstNum;

range is the total number you want to generate
firstnum is the beginning number of that range

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

What Walt P said basically sums it up =)

I took off my answer so you could think a little bit harder for yourself =P

skorm909
Junior Poster
111 posts since Feb 2010
Reputation Points: 10
Solved Threads: 3
 

Sometimes 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 100

const int max = 100;
const int min = -100;
int r4 = rand() % (max - min) + min; // random number between [min,max)


When you want to look up a function, you can see examples here . Bookmark it if you need to. Its a good reference

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

Sometimes 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 100

const int max = 100;
const int min = -100;
int r4 = rand() % (max - min) + min; // random number between [min,max)

When you want to look up a function, you can see examples here . Bookmark it if you need to. Its a good reference

Thanks! The example worked well! I figured it out now.

rfrapp
Junior Poster in Training
57 posts since Dec 2011
Reputation Points: 27
Solved Threads: 0
 

Sometimes 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 100

const int max = 100;
const int min = -100;
int r4 = rand() % (max - min) + min; // random number between [min,max)

When you want to look up a function, you can see examples here . Bookmark it if you need to. Its a good reference

How do i mark a thread solved? I'm relatively new here :p

rfrapp
Junior Poster in Training
57 posts since Dec 2011
Reputation Points: 27
Solved Threads: 0
 

by the bottom before the text box, there's some text then you can click: Mark this Thread as Solved

skorm909
Junior Poster
111 posts since Feb 2010
Reputation Points: 10
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You