Random selection of numbers

I am trying to have my program randomly select a number from the numbers 4, 5, and 8. These numbers come from the length of 3 strings I have. You can see the code below. Can anyone please assist me in going about this?

1
2
3
4
5
6
7
8
9
int lengthR;
int lengthP;
int lengthS;
string Rock = "Rock";
string Paper = "Paper";
string Scisors = "Scissors";
lengthR = Rock.size();
lengthP = Paper.size();
lengthS = Scisors.size();
It can be done by making a random select function, which generates a random number from one to three and then, according to the generated number returns a specified number.
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
#include<iostream>
#include<random>   //for rand() and srand()
#include<time.h>    //for time()
using namespace std;
int random_select(){  //function for randomized selection
int n = 1+rand()%3;  //here, it gives to n a random value from 1 to 3
if (n==1){
    return 4;
}
else if(n==2){
    return 5;
}
else if(n==3){
    return 8;
}
}
int main(){
srand(time(NULL));
string rock = "rock";
string paper = "paper";
string scissors = "scissors";
int rock_l = rock.size();
int paper_l = rock.size();
int scissors_l = scissors.size();
int checker = random_select();
if (chcker == rock_l){
    //do whatever
}
else if(checker == paper_l){
    //do whatever
}
else{
    //do whatever
}
}
There's a lot of repetition in your code. It could be a good place to use arrays. But it may depend on where you are heading next with this code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>


using namespace std;

int main()
{
    srand(time(0));
    
    string items[3] = { "Rock", "Paper", "Scissors"};
    
    int r = rand() % 3;
    
    cout << items[r] << " length is " << items[r].size() << '\n';
    
}


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
#include <iostream>
#include <time.h>

using namespace std;

int RandomNumber()
{
  int num;

  do
  {
    num = rand();
  } while (num != 4 && num != 5 && num != 8);

  return num;
}

void main(void)
{
  srand(time(0));
  int num = RandomNumber();  
  cout << "Num = " << num << "\n\n";
  
  system("pause");
}
@Thomas1965
I believe your code is quite impractical. I've made a code to see the probability of using simple rand() without limits and I've ran it 10-20 times.
1
2
3
4
5
6
7
8
9
10
11
int main(){
srand(time(NULL));
int counter = 0;
for (int i=0; i<100000; i++){
    int k = rand();
    if (k==4 || k==5 || k==8){
        counter++;
    }
}
cout<<counter;
}

Counter takes numbers in the range from 1 to 20. So, you're while loop can run 100000 times before it finds 4 or 5 or 8.
I'd suggest
1
2
3
4
5
int RandomNumber()
{
    static const int choice[3] = {4, 5, 8};
    return choice[rand() % 3];
}
rather than using a while loop.

This is effectively the same as random_select() in jgg2002's post above, but perhaps more practical if there are a large number of options.
Last edited on
@jpp2002,

when writing some code for a beginner my primary aim is that it is easy to understand.
To test I ran the function 100 times and the results appeared instantaneous on the screen.
I prefer Chervil's solution which is compact and efficient, but for a beginner it is not so obvious how it works.
Topic archived. No new replies allowed.