How to randomly pick a variable

Pages: 12
May 1, 2011 at 8:49pm
Hi, I'm new to C++ and cplusplus.com. I have come across a problem that I do not know how to perform.

What I am trying to do is that I want to enter in some words and assign it to my variables (I already got that down). Now I want to make it so that the program randomly chooses between the variables.

Example: I type in 'Orange' and 'Apple'. I want the program to randomly pick Orange or Apple.

So is this possible? If so, how? Also, if there's already a thread that I missed, please show me it.

Thanks.
May 1, 2011 at 8:52pm
You can make an array of variables and use rand() to pick one of them =]
May 1, 2011 at 8:53pm
Ah okay. Would it be possible if you gave me an example or something?

It's alright if I have to research it myself. I learn faster and better when seeing an example.
May 1, 2011 at 8:53pm
There are many ways to do this.

One way is:
1. store the entries in a vector<string> called variables
2. randomly pick a number i between 0 and variables.size()-1
3. your result is variables[i]

May 1, 2011 at 8:56pm
Oh, okay. As I am still new to C++, I don't really know what you just said. (Sorry :( )
This is what I have so far. (Not much :'( )

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
  char This[0], That[1]; //This statement identifies 'This' and 'That' as a character string
  std::cout << "Please type in two things that you want to do, but can't decide." << std::endl << "Note: DO NOT ADD A SPACE TO YOUR DECISION!" << std::endl << std::endl << "Example: 'My First Decision'" << std::endl << "Instead do this to avoid errors" << std::endl << "'MyFirstDecision'" << std::endl << std::endl;
  std::cin >> This;
  std::cin >> That;
  
  
  
  return 0;
}

Last edited on May 1, 2011 at 9:06pm
May 1, 2011 at 9:12pm
Use this in learning how to pick a random number and use it for your output.

http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

May 1, 2011 at 9:13pm
Okay, thanks guys!
May 1, 2011 at 9:18pm
You're welcome :) Good luck!
May 1, 2011 at 9:37pm
Hmm.. This is a lot more difficult than I thought! What I have tried is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>

int main()
{
  char This[0], That[1]; //This statement identifies 'This' and 'That' as a character string
  int randomDecision;
  
  randomDecision = rand();
  
  std::cout << "Please type in two things that you want to do, but can't decide." << std::endl << "Note: DO NOT ADD A SPACE TO YOUR DECISION!" << std::endl << std::endl << "Example: 'My First Decision'" << std::endl << "Instead do this to avoid errors" << std::endl << "'MyFirstDecision'" << std::endl << std::endl;
  std::cin >> This;
  std::cin >> That;
  
  std::cout << randomDecision << std::endl;
  
  return 0;
}


It obviously does not work. I have tried changing

randomDecision = rand();

to

randomDecision = rand([0], [1]);

but that does not work. I am not fully understanding how rand works. Please, help me?

EDIT: Is there a way to make rand pick from [0] (this) and [1](that)?
Last edited on May 1, 2011 at 9:38pm
May 1, 2011 at 9:44pm
char This[0], That[1]; //This statement identifies 'This' and 'That' as a character string


This is all wrong. It doesn't do what you think it does. It creates two arrays of characters, one of size zero and one of size 1. Basically neither one of them are large enough to hold a string.

If you want a string, make a string. Forget about char:

1
2
3
4
5
#include <string>  // include string for strings

int main()
{
  std::string This, That;  // makes 2 strings 


although for your purposes... it's easier if they're an array or vector:

 
std::string options[2];  // two options 



rand() returns a number between [0..RANDMAX]. You can use the mod operator % to make a number between [0..x) where x is whatever number you mod by.

Say for example you want 0 or 1:

 
int r = rand() % 2;  // a random number, either 0 or 1 




Then if you want to turn that 0 or 1 into the options they gave you, just use 'r' to index your array:

 
std::cout << "Random selection:  " << options[r];
Last edited on May 1, 2011 at 9:44pm
May 1, 2011 at 9:48pm
Thanks Disch! Hopefully I won't have anymore problems..
May 1, 2011 at 9:52pm
This most important issue that you have is you are declaring character arrays incorrectly. char this[0]; declares an array of characters the size of 0. It can't store anything. char This[0], That[1]; declares 2 different character arrays, this with a size of 0, that with a size of 1. char This[2][20]; declares a single array containing 2 char arrays of size 2.
1
2
3
4
char This[2][20];
This[0]//access the first array
This[1]//access the second array
This[0][5]//Access the 6th element of the first array 


I suggest you look into Strings.

Second:


rand()%2 will randomly pick a number between 0 and 1, inclusive. So that:

1
2
char This[2][20];
This[rand()%2];
will randomly pick an array.
Last edited on May 1, 2011 at 10:48pm
May 1, 2011 at 10:28pm
Hm, okay so this is now kind of confusing since both of you gave me different things to do. But here's what I got so far..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>
#include <string>

int main()
{
  std::string options[1];
  std::cout << "Please type in two things that you want to do, but can't decide." << std::endl << "Note: DO NOT ADD A SPACE TO YOUR DECISION!" << std::endl << std::endl << "Example: 'My First Decision'" << std::endl << "Instead do this to avoid errors" << std::endl << "'MyFirstDecision'" << std::endl << std::endl;
  
  std::cin >> options[0];
  std::cin >> options[1];
  
  std::string r = options[rand() % 2];
  
  std::cout << "You should: " << options[r];
  return 0;
}


This gives me an error when I compile (oh by the way, I'm using MinGW)

C:\Users\Fruits\Desktop>g++ -Wall -o ShouldIDoThisOrThat ShouldIDoThisOrThat.cpp
ShouldIDoThisOrThat.cpp: In function 'int main()':
ShouldIDoThisOrThat.cpp:15:43: error: no match for 'operator[]' in 'options[r]'

EDIT: Also, wouldn't std::string options[2]; // two options that be three options?
Last edited on May 1, 2011 at 10:30pm
May 1, 2011 at 10:52pm
Ah ha I messed up so bad, sorry, but I fixed it.

Anyways, the difference is

std::string options[1]; is an array of just 1, not two.
1
2
std::string options[1];
options[1] = "Some String";//this is illegal, options[1] is out of bounds 

The number you declare it with is the number of elements you can store. So if you do std::string options[5]; it can hold only 5 strings, 0-4 inclusive.

Sorry if I confused you.

Also, your compiler error is because on line 13 you declare r as a string, it now contains what you typed in, so on line 15 options[r]; is like saying options["Apple"]

Your line 15 could work like: std::cout << "You should: " << r;
May 1, 2011 at 11:06pm
Okay, so my code is now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>
#include <string>

int main()
{
  std::string options[1];
  std::cout << "Please type in two things that you want to do, but can't decide." << std::endl << "Note: DO NOT ADD A SPACE TO YOUR DECISION!" << std::endl << std::endl << "Example: 'My First Decision'" << std::endl << "Instead do this to avoid errors" << std::endl << "'MyFirstDecision'" << std::endl << std::endl;
  
  std::cin >> options[0];
  std::cin >> options[1];
  
  std::string r = options[rand() % 2];
  
  std::cout << "You should: " << r;
  return 0;
}


Gives no error, but when i execute it, and enter in Orange and Apple, the program crashes. So I changed

std::string options[1];

to

std::string options[2];

but now it just always outputs Apple.
May 1, 2011 at 11:12pm
Ah ha I am so bad at this looking at other peoples code thing

You need to seed the random number generator, throw this code in on the top time of main srand(time(0));

You may or may not need to include ctime.h to use that. What that does is seeds the random number generator based on the current time.
May 1, 2011 at 11:36pm
EDIT: Wait! I forgot, is it possible to make it so that when I input something, it allows spaces? So something like 'The Apple Tree'. Because whenever I input 'The Apple Tree', it just immediately goes to the end and says 'You Should: Apple'

Sorry for the long reply! And now.... It works!!

Thank you so much guys! I have learned a lot from this little project I made up. :)

Once again, thanks!

Last edited on May 1, 2011 at 11:39pm
May 1, 2011 at 11:50pm
Yes, use getline:

1
2
string yourstring
getline( cin, yourstring );  // puts the whole line in yourstring, including spaces 
May 1, 2011 at 11:55pm
Okay, I don't know if I'm doing this right because this is a new command, but my program crashes as soon as I start it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>

int main()
{
  srand(time(0));
  std::string options[2];
  std::cout << "Please type in two things that you want to do, but can't decide." << std::endl << "Note: DO NOT ADD A SPACE TO YOUR DECISION!" << std::endl << std::endl << "Example: 'My First Decision'" << std::endl << "Instead do this to avoid errors" << std::endl << "'MyFirstDecision'" << std::endl << std::endl;
  
  std::getline(std::cin, options[2]);
  
  std::string r = options[rand() % 2];
  
  std::cout << "You should: " << r;
  return 0;
}


EDIT:Ahh, sorry, I fixed it myself! I forgot that you have to ask two times! Sorry!
Also, thanks guys! I learnt a lot.

Changed

std::getline(std::cin, options[2]);

to

1
2
std::getline(std::cin, options[0]);
std::getline(std::cin, options[1]);
Last edited on May 2, 2011 at 12:15am
May 2, 2011 at 1:05am
Glad to help. :)
Pages: 12