rand( )
Code: C
hello , I am having trouble understanding the program from (generate the numbers). please explain it dissecting the code from there ! thnx 
Re: rand( )
The program works this way :
(1) Creates a vector that stores the number of repetitions of the generated numbers. number[i] stores the number of times the integer i is generated. (2) This loop basically generates the numbers and keeps track of repetitions : Code: C
(3) Output section simply shows how many times each number was generated. 
Re: rand( )
thnx, but can u please make me understand :
numbers[ rand( ) % DIFFERENT_NUMBERS + 1 ]++ ; for example, the random no. chosen is 5 then wat r the next steps taken. 
Re: rand( )
OK. Let the rand() return 2574.
As DIFFERENT_NUMBERS = 10, rand( ) % DIFFERENT_NUMBERS = 4. So, rand( ) % DIFFERENT_NUMBERS + 1 = 4 + 1 = 5. So, the statement finally becomes : numbers[5]++ i.e. the count of '5' increases by one. This is repeated 'totalNumbers' times. Let the user enter 'totalNumbers' as 7. So, 7 random numbers are chosen within 1 ~ 10 : say  1, 7, 8, 2, 3, 1, 7. So, the numbers array will have the following content : numbers[1] = 2 numbers[2] = 1 numbers[3] = 1 numbers[4] = 0 numbers[5] = 0 numbers[6] = 0 numbers[7] = 2 numbers[8] = 1 numbers[9] = 0 numbers[10] = 0 I hope it's clear now :) 
Re: rand( )
thank you ! :pleased::pleased::pleased:

Re: rand( )
My pleasure !! :)

All times are GMT +5.5. The time now is 13:40. 