0
int main() 
{ 
  srand((unsigned)time(0)); 
  int random_integer; 
  int lowest=0, highest=10; 
  int range=(highest-lowest)+1; 
  for(int index=0; index<20; index++){ 
    random_integer = (rand() % range) + lowest/(RAND_MAX + 1.0);
    cout << random_integer << endl; 
  } 
} 

I am getting the output from 0 to 10, 11 numbers, but I don't want to get number 10, just numbers 0 to 9, that means 10 random numbers, what should I do?

LihO
  • 41,190
  • 11
  • 99
  • 167
visanio_learner
  • 373
  • 3
  • 4
  • 12
  • 1
    Remove the `+ 1` from the range evaluation. – Alexander Mar 01 '12 at 11:47
  • 2
    change `highest=10` to `highest=9` ? i wouldnt change the code any other way since your variables give the impression that it should give "highest" number of 10... which wont work anymore if you follow Alexander's advice. – Rookie Mar 01 '12 at 11:48
  • possible duplicate of [Generating random integer from a range](http://stackoverflow.com/questions/5008804/generating-random-integer-from-a-range) – mmmmmm Mar 01 '12 at 11:49
  • @Alexander, it works perfectly, but i wanna know what that +1 means for the code there... could u please explain it. actual problem – visanio_learner Mar 01 '12 at 11:52

5 Answers5

3

Modulo operation x % c; returns the remainder of division of number x by c. If you do x % 10 then there are 10 possible return values: 0 1 2 3 4 5 6 7 8 9.

Note that generating random numbers by using rand() with % produces skewed results and those numbers are not uniformly distributed. Here's the simple C-style function that generates random number from the interval from min to max, inclusive:

int irand(int min, int max) {
    return ((double)rand() / ((double)RAND_MAX + 1.0)) * (max - min + 1) + min;
}

Note, that numbers generated by this functions are uniformly distributed:

int occurences[8] = {0};

srand(time(0));
for (int i = 0; i < 100000; ++i)
    ++occurences[irand(1,7)];

for (int i = 1; i <= 7; ++i)
    cout << occurences[i] << ' ';

output: 14253 14481 14210 14029 14289 14503 14235

Also have a look at:
Generate a random number within range?
Generate random numbers uniformly over an entire range
What is the best way to generate random numbers in C++?

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
  • okay, but what you're expecting to get, is it 8 random numbers or just 7... because my question is all about the count of numbers that i get... i should only get 10 numbers from lowest being 0 and highest being 10... – visanio_learner Mar 01 '12 at 11:55
  • 1
    @visanio_learner: If you call my function like this: `irand(0,3)`, there are 4 possible return values: `0, 1, 2, 3`. Hope it's clear now ;) – LihO Mar 01 '12 at 11:58
  • @visanio_learner: Thank you. But someone dowvoted it and I'd like to know the reason. – LihO Mar 01 '12 at 12:54
  • @visanio_learner: I am not asking you. I just hope someone will read my comment and write the reason he downvoted... – LihO Mar 01 '12 at 12:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8393/discussion-between-visanio-learner-and-liho) – visanio_learner Mar 01 '12 at 12:59
0

It appears that you need to fully understand, then modify this line

random_integer = (rand() % range) + lowest/(RAND_MAX + 1.0);
dklt
  • 1,703
  • 1
  • 12
  • 12
0

rand() % 10 normally does the trick for me.. (unless I'm missing something)

Nim
  • 33,299
  • 2
  • 62
  • 101
0
int main() 
{ 
  srand((unsigned)time(0)); 
  int random_integer; 
  int lowest=0, highest=9; 
  int range=(highest-lowest)+1; 
  for(int index=0; index<20; index++){ 
    random_integer = (rand() % range) + lowest;
    cout << random_integer << endl; 
  } 
} 
SigTerm
  • 26,089
  • 6
  • 66
  • 115
0

this type of questions have been answered n number of times.

have a look at this

Community
  • 1
  • 1
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112