Random sequence

Need help with this problem I can't seem to get the concept of the random sequence occurrence.


Create a function/method/procedure that
returns a random number from the following set,
{19,62,88,101,121}. Loop 10000 times with this
procedure and print the frequency of each of
the 5 numbers obtained. Hint: Use an array for
the sequence and frequency.



Setup
const int n=5,ntimes=10000;
int freq[n]={0};
short int rndseq[]={19,62,88,101,121};


Call to the routine to return a random sequence
retrand(rndseq,freq,n,ntimes);


Results
cout<<rndseq[i]<<" occurred "<<
freq[i]<<" times"<<endl;


Sample Output
19 occurred 2055 times
62 occurred 1986 times
88 occurred 1962 times
101 occurred 2079 times
121 occurred 1919 times
Last edited on
The way I would do it is to make the function return the index of the random number in the set, and not the number itself. Then the calling function can increment the number at that index in the freq array.

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
     for(int i = 0; i < ntime; i++)
     {
       int index = retrand(n); // pass array size to function, which returns a random index within the array (0 to n-1)
       freq[index]++; // increment the number at the index in freq array
     }

   // print out frequency for each number
   for(int i = 0; i < n; i++) cout<<rndseq[i]<<" occurred "<< freq[i]<<" times"<<endl;
    
}


All you have to do now is implement the retrand function. Which should't be hard if you know how to use rand().
Last edited on
im using the retrand function like this at the end of the code but it's not working

int retrand(int rndseq, int n){
    return rand() % n+1;
}
Last edited on
You need to return a random number between 0 and n-1 (inclusive). So it would be like this:

1
2
3
int retrand(int n){
    return rand() % n; // random number between 0 and n-1
}


Notice, I modified the function a little. It turns out we dont even need to pass the array itself. The size of the array (n) is whats necessary, as we are simply getting a random index from 0 to n-1.

Note that you should seed the random number generator in the beginning of main.
Last edited on
Alright i tried your code but i still cant make it work. i added the random number generator and the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//System Libraries
#include <iostream>//Input/Output Stream Library
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;  //Namespace of the System Libraries

//User Libraries

//Global Constants

//Function Prototype 
int indexFinder(int retrand[]);
int retrand (int);

int main(int argc, char** argv){
    srand(static_cast<int>(time(0)));
    const int n=5,ntimes=10000;
    int freq[n]={0};
    const int rndseq[n]={19,62,88,101,121};
    
     for(int i = 0; i < ntimes; i++)
     {
       int index = retrand(rndseq, n); // pass array and size to function, which returns a random index within the array
       freq[index]++; // increment the number at the index in freq array
     }

   // print out frequency for each number
   for(int i = 0; i < n; i++) 
   cout<<rndseq[i]<<" occurred "<< freq[i]<<" times"<<endl;
  
   
   return 0;
   }                               
int retrand(int n){
    return rand() % n; // random number between 0 and n-1
}
> i still cant make it work
¿can't you be a little more descriptive?
For example, it fails to compile
foo.cpp:24:32: error: invalid conversion from ‘const int*’ to ‘int’ [-fpermissive]
foo.cpp:24:32: error: too many arguments to function ‘int retrand(int)’



Then you may read Arslan7041's post
It turns out we dont even need to pass the array itself. The size of the array (n) is whats necessary
int index = retrand(n);
Im so blind lol Thanks guys fixed it!!!
Topic archived. No new replies allowed.