Can I pick a random element from an array in C++? - c++

This has been bugging me for days:
#include <iostream>
using namespace std;
string words[] = {"cake", "cookie", "carrot", "cauliflower", "cherries", "celery"};
string word = words[rand() % 6];
string guess;
int lives = 3;
int main()
{
std::cout << "Can you guess what word I'm thinking of? I'll give you a hint: it's a food that starts with the letter C. You have three tries. Good luck!" << std::endl;
while(lives > 0)
{
std::cin >> guess;
std::cout << std::endl;
if(guess == word)
{
std::cout << "Wow, That's actually correct! Good job!" << std::endl;
break;
}
else
{
lives--;
std::cout << "Nope! You now have " << lives << " lives left." << std::endl;
}
}
if(lives <= 0)
{
std::cout << "And... you lose!" << std::endl;
}
return 0;
}
I'm currently working on a word-guessing game, but when I try to pick a random element from my words array, it gets stuck on element 1 (i.e "cookie"). I used:
string words[] = {"cake", "cookie", "carrot", "cauliflower", "cherries", "celery"};
string word = words[rand() % 6];
Help would be appreciated.

If you want to do it C++ style, use , and for maintenance I think std::vector of std::string is a good choice too.
#include <iostream>
#include <vector>
#include <string>
#include <random>
int main()
{
std::vector<std::string> words{ "cake", "cookie", "carrot", "cauliflower", "cherries", "celery" };
// gets 'entropy' from device that generates random numbers itself
// to seed a mersenne twister (pseudo) random generator
std::mt19937 generator(std::random_device{}());
// make sure all numbers have an equal chance.
// range is inclusive (so we need -1 for vector index)
std::uniform_int_distribution<std::size_t> distribution(0, words.size() - 1);
for (std::size_t n = 0; n < 40; ++n)
{
std::size_t number = distribution(generator);
std::cout << words[number] << std::endl;
}
}

rand() is a pseudo random number generator. That means, given the same starting conditions (seed), it will generate the same pseudo random sequence of numbers every time.
So, change the seed for the random number generator (e.g. use the current time as a starting condition for the random number generator).
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
static const string words[] = {"cake", "cookie", "carrot", "cauliflower", "cherries", "celery"};
int main() {
//variables moved to inside main()
string guess;
int lives = 3;
srand(time(NULL));
string word = words[rand() % 6];

if u didnt generate using a srand() function
ur programm will automatic generate for you with the seed 1
so you can proceed it like this:
#include<cstdlib>
#include<ctime>
...
int main()
{
srand(time(NULL));
....
}

You shouldn't use rand in C++, there are far better random engines.
#include <iostream>
#include <array>
#include <random>
using namespace std;
// use std::array as a better alternative to C arrays
array words {"cake", "cookie", "carrot", "cauliflower", "cherries", "celery"};
string guess;
int lives = 3;
int main()
{
std::mt19937 gen{std::random_device{}()}; // generates random numbers
std::uniform_int_distribution<std::size_t> dist(0, words.size() - 1); // maps the random number to [0..number of words]
int index = dist(gen);
string word = words[index];
...
}

Related

Generate random letter array and count occurences

Hello I am trying to generate a random array of the length that the user inputs. My array should then print and display the occurences of those letters in the array. So far this only prints up to the letter g and the occurences are incorrect. If someone could tell me what I am doing wrong it would help alot. Thank you.
#include <iostream>
#include <cstring>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(0));
int i, num;
char ch;
char chars[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int freq[26]={0};
cout << "How many letters do you want in your string? ";
cin >> num;
for (i=0; i < num; i++)
{
ch = chars[rand()%26];
chars[i]=ch;
freq[i] +=1;
cout << ch;
}
for (char lower = 'a'; lower <='z'; lower++)
{
cout << "\nLetter" << lower << "is " << freq[lower] << "times";
}
}
Problem 1
The lines
chars[i]=ch;
freq[i] +=1;
are not right. You need to use:
int index = ch - 'a';
freq[index] += 1;
Problem 2
The index in the for loop for printing the data is not correct either.
You need to use:
for (char lower = 'a'; lower <='z'; lower++)
{
int index = lower - 'a';
cout << "\nLetter" << lower << "is " << freq[index] << "times";
}
Important Note
It is worth noting that the C++ standard does not guarantee that lower case letters are contiguous. (Thanks #MartinBonner). For instance, if your system uses EBCDIC encoding your program won't work.
To make your code robust, it will be better to use a std::map.
int main()
{
srand(time(0));
int i, num;
char ch;
char chars[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
std::map<char, int> freq;
// Initialize freq.
for ( ch : chars )
{
freq[ch] = 0;
}
cout << "How many letters do you want in your string? ";
cin >> num;
for (i=0; i < num; i++)
{
ch = chars[rand()%26];
freq[ch] +=1;
}
for (auto item : freq )
{
cout << "\nLetter" << item.first << "is " << item.second << "times";
}
}
You might wanna give a look to C++11 Pseudo-random number generation here is a short way of generating the range that you want using this:
#include <algorithm>
#include <array>
#include <random>
#include <vector>
using namespace std;
int main()
{
int arraySize = 35;
mt19937 engine{random_device{}()};
uniform_int_distribution<> dist{'a', 'z'};
vector<char> vec;
generate_n(back_inserter(vec), arraySize, [&]() { return static_cast<char>(dist(engine); }));
//To count occurrences
array<int, 26> freq;
for (auto c : vec) { ++freq[c-'a']; }
return 0;
}
You should not write into chars, and freq should be extended to cover the a...z range (the ASCII codes), which it does not. Also, increase at index ch, not at i.
I do not even know that range from the top of my head, but it could be modified to track all possible bytes instead (0...255), see result on https://ideone.com/xPGls7
List of changes:
int freq[256]={0}; // instead of int freq[26]={0};
// chars[i]=ch; is removed
freq[ch] +=1; // instead of freq[i] +=1;
Then it works.
Using lambda functions to do most of the work.
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <ostream>
#include <random>
#include <string>
#include <utility>
#include <vector>
using namespace std::string_literals;
int main()
{
std::mt19937::result_type seed = std::random_device{}();
auto engine = std::mt19937(seed);
auto dist = std::uniform_int_distribution<>('a', 'z');
auto random_letter = [&engine, &dist]() { return static_cast<char>(dist(engine)); };
std::cout << "How many letters do you want to generate? "s;
int n;
if (!(std::cin >> n)) { return EXIT_FAILURE; }
auto letters = std::vector<char>();
std::generate_n(std::back_inserter(letters), n, random_letter);
auto zero = std::map<char, int>();
auto const frequencies = std::accumulate(std::cbegin(letters), std::cend(letters), zero,
[](auto& acc, auto c)
{
++acc[c];
return acc;
});
for (auto const [c, freq] : frequencies)
{
std::cout << "The letter '"s << c << "' appeared "s << freq << " times." << std::endl;
}
return 0;
}

C++ random number generator only generates 0 - C++ 11

I am trying to generate a random number between 0 and 2 (inclusive) for a C++ program:
#include <iostream>
#include <string>
#include <cstdlib>
#include <random>
using namespace std;
int generateRowMovement(){
//http://www.cplusplus.com/reference/random/uniform_int_distribution/
default_random_engine generator;
uniform_int_distribution<int> distribution(0,2);
int row = distribution(generator);
return row;
}
int main(){
int row = generateRowMovement();
cout << row << endl;
int row2 = generateRowMovement();
cout << row2 << endl;
int row3 = generateRowMovement();
cout << row3 << endl;
return 0;
}
All 3 results are:
0
0
0
I have been testing this for 30 minute and it is always zero. I have tried following this link and this SO post (which posts link back to), but none of their solution is fix this problem. How can I generate 0, 1, and 2?
There are two issues with the code you posted that keeps it from working how you intend it to. The first is that generator is never given a seed, or as is commonly shown online, a random_device, ie
random_device rd;
default_random_engine generator(rd());
The second is that every time you call the function, you're creating a new random engine, and a new distribution. It would be better to create a random device, give it to to the engine only once, and then let your function return the values based on those.
#include <iostream>
#include <string>
#include <cstdlib>
#include <random>
using namespace std;
random_device rd;
default_random_engine generator(rd());
uniform_int_distribution<int> distribution(0,2);
int generateRowMovement(){
return distribution(generator);
}
int main(){
int row = generateRowMovement();
cout << row << endl;
int row2 = generateRowMovement();
cout << row2 << endl;
int row3 = generateRowMovement();
cout << row3 << endl;
return 0;
}

How to create an array of normal distributed random number generators in c++?

I need a series of normally distributed random numbers, with different mean and variance, I know how to create one series with a particular mean and variance but can I have like an array of generators?
like for 1 series we have
#include <random>
#include <iostream>
using namespace std;
int main()
{
random_device rd;
mt19937 gen(rd());
normal_distribution<double> d(0.0, 1.0);
for (int i = 0; i < 5000; i++)
cout << " " << d(gen) << "\n";
return 0;
}
and this gives me a series of normally distributed random numbers, and i know i can create another d with another mean and variance for another series but is there any way to have a lot of such normal_distribution d together in an array so that i can choose a particular generator by simply choosing an element in the array.
I have tried a version where
#include <random>
#include <iostream>
using namespace std;
int main()
{
random_device rd;
mt19937 gen(rd());
normal_distribution<double> d(0.0, 1.0),d2(0.0,1.0);
normal_distribution<double> D[]={d,d2};
for (int i = 0; i < 5000; i++)
cout << " " << D[0](gen) << "\n";
system("pause");
return 0;
}
but I want to initialize it directly with the array like D0 some thing like that so that i can put it in a loop
Sure you can
#include <iostream>
#include <vector>
#include <random>
int main() {
std::vector<std::normal_distribution<double>> D{
std::normal_distribution<double>{0.0, 1.0 },
std::normal_distribution<double>{0.0, 2.0 } };
std::random_device rd;
std::mt19937 gen(rd());
std::cout << D[0](gen) << "\n";
std::cout << D[1](gen) << "\n";
return 0;
}
As you know C++ provide a functions for random numbers and we can also create and initialize array as given below. I hope it will helpful if not comment below.
const int nrolls=10000; // number of experiments
const int nstars=100; // maximum number of stars to distribute
std::default_random_engine generator;
std::normal_distribution<double> distribution(5.0,2.0);
int p[10]={};
for (int i=0; i<nrolls; ++i) {
double number = distribution(generator);
if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
}
std::cout << "normal_distribution (5.0,2.0):" << std::endl;
for (int i=0; i<10; ++i) {
std::cout << i << "-" << (i+1) << ": ";
std::cout << std::string(p[i]*nstars/nrolls,'*') << std::endl;
}

Error/Exception catching within a class

I have written a class that returns a random string after you provide a seed, a sequence length, and a character set as shown below.
I would like to know how to catch any bad input, such as setting the seed to 0 or to a negative number.
The examples that I have seen have just used cout to send an error message and then exit(1); I am thinking about putting a bool flag as a private variable and setting it to false when invalid input is detected. Then after you attempt to generate a random string with this class you would just check that flag through an accessor function.
Is there a better and/or standard way of handling this that will not just exit the program like exit(1)? Any general input about the class is welcomed also. Thanks for the assistance.
RandomString.h
// This is the header file randomstring.h. This is the interface for the class RandomString.
// Values of this type are a random string of the specified length from the specified string character set.
// The values that are needed for input are a positive integer seed, an integer desired length, and a string character set.
// Uses the mt19937 random number engine with a uniform_int_distribution.
#ifndef RANDOMSTRING_H
#define RANDOMSTRING_H
#include <string>
#include <random>
using namespace std;
namespace RandomString
{
class RandomString
{
public:
RandomString(double newSeed, unsigned int newLength, string newCharacterSet); // Initializes the RandomString object with the provided arguments.
RandomString(); // Initializes the seed to 1, the length to 0, and the character set to '0'.
double getSeed();
unsigned int getLength();
string getCharacterSet();
string getSequence();
void setSeed(double newSeed); // Sets the new seed but does not produce a new random sequence.
void setLength(unsigned int newLength); // This is the length of randomSequence.
void setCharacterSet(string newCharacterSet);
void generateNext(); // Generates the next random sequence.
private:
double seed;
unsigned int length;
string characterSet;
string randomSequence;
mt19937 engine;
};
} // RandomString namespace
#endif
RandomString.cpp
// This is the implementation file randomstring.cpp. This is the implementation for the class RandomString.
// The interface for the class RandomString is in the header file randomstring.h.
#include "stdafx.h"
#include <string>
#include <random>
#include "randomstring.h"
using std::string;
using std::uniform_int_distribution;
namespace RandomString
{
RandomString::RandomString(double newSeed, unsigned int newLength, string newCharacterSet)
{
setSeed(newSeed);
setLength(newLength);
setCharacterSet(newCharacterSet);
}
RandomString::RandomString()
{
seed = 1;
length = 0;
characterSet = '0';
}
double RandomString::getSeed()
{
return seed;
}
unsigned int RandomString::getLength()
{
return length;
}
string RandomString::getCharacterSet()
{
return characterSet;
}
string RandomString::getSequence()
{
return randomSequence;
}
void RandomString::setSeed(double newSeed)
{
seed = newSeed;
engine.seed(seed);
}
void RandomString::setLength(unsigned int newLength)
{
length = newLength;
}
void RandomString::setCharacterSet(string newCharacterSet)
{
characterSet = newCharacterSet;
}
void RandomString::generateNext()
{
randomSequence.resize(length);
uniform_int_distribution<> distribution(0,characterSet.length() - 1);
for (int i = 0; i < length; i++)
{
randomSequence[i] = characterSet[distribution(engine)];
}
}
} // RandomString namespace
Finally, here is the test program that I am using.
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "randomstring.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
do
{
double seed = 0;
unsigned int length = 0;
cout << "Enter seed: ";
cin >> seed;
cout << "Enter length of string: ";
cin >> length;
cout << endl;
RandomString::RandomString randomTest(seed,length,"ABCDEFGHJKLMNPQRSTUVWXYZ1234567890");
cout << "class RandomString" << endl;
cout << "seed: " << randomTest.getSeed() << endl;
cout << "length: " << randomTest.getLength() << endl;
randomTest.generateNext();
cout << "random sequence: " << randomTest.getSequence() << endl;
randomTest.generateNext();
cout << "next random sequence: " << randomTest.getSequence() << endl << endl;
}while(true);
}
Throw an exception
void RandomString::setSeed(double newSeed)
{
if (newSeed <= 0) {
throw std::runtime_error("seed should be positive");
}
seed = newSeed;
engine.seed(seed);
}
User would have to use try catch to handle error, else std::terminate is called.
How about letting the user have another chance at giving a good input?
What is the next thing the user will do after they find out they have put in a bad input? There is a high chance they will try again with a good input so just ask for another one.
while(true)
{
cout << "Enter seed: ";
cin >> seed;
if(seed > 0)
{
cout << "ERROR: Seed must be greater than 0." << endl;
break;
}
}
To answer your question about the bool error flag, I wouldn't do it that way. I have no need for bad data that was generated from a bad seed. No data is better than bad data. I generally try to "fail early, fail often", meaning I should stop and report the error as soon as I realize it exists.
If you were set on storing it the error flag, I'd have generateNext() or setSeed return false rather than nothing if there was a problem. Exceptions also work, but are slower and some projects don't like them in C++.
If you plan to have others use RandomString, I'd change the method signature from a double to uint (unsigned int). This would catch negative numbers being used at compile time rather than run time...again helping to fail even earlier. Sounds like this might not work in your classroom scenario, but is a good principle in the real world.

I would like to count the numbers in a string /c++

I have tried to count the numbers in a string but it doesnt work and I think it is logically good. I am a beginner in programming.
I know it works for one-digit numbers but that's intentional.
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int main()
{
int numbs [10] = {0,1,2,3,4,5,6,7,8,9};
string str1;
cin >> str1;
vector <unsigned int> positions;
for (int a = 0 ;a <=10;a++)
{
int f = numbs[a];
string b = to_string(f);
unsigned pos = str1.find(b,0);
while(pos !=string::npos)
{
positions.push_back(pos);
pos = str1.find(b,pos+1);
break;
}
}
cout << "The count of numbers:" << positions.size() <<endl;
return 0;
}
If you need only to count digits in a string then there is no sense to use std::vector. You can count them without the vector. For example
#include <iostream>
#include <string>
int main()
{
std::string s( "A12B345C789" );
size_t count = 0;
for ( std::string::size_type pos = 0;
( pos = s.find_first_of( "0123456789", pos ) ) != std::string::npos;
++pos )
{
++count;
}
std::cout << "The count of numbers: " << count << std::endl;
return 0;
}
The output is
The count of numbers: 8
Also you could use standard algorithm std::count_if defined in header <algorithm>
For example
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
std::string s( "A12B345C789" );
size_t count = std::count_if( s.begin(), s.end(),
[]( char c ) { return std::isdigit( c ); } );
std::cout << "The count of numbers: " << count << std::endl;
return 0;
}
The output is
The count of numbers: 8
If you need to count numbers instead of digits in a string then you should use standard C function strtol or C++ function std::stoi
Use substrings to extract every part of string with a delimiter(normally a space). Then convert each substring to number. The ones that qualify and converts probably are the numbers in your string. See how many you get.
You might also be interested in the C++ function "isdigit":
http://www.cplusplus.com/reference/locale/isdigit/
For example:
include <iostream>
#include <string.h>
#include <vector>
#include <locale> // std::locale, std::isdigit
using namespace std;
int main()
{
// Initialze array with count for each digit, 0 .. 9
int counts[10] = {0, 0, 0, 0, 0, 0, 0,0, 0, 0 };
int total = 0;
// Read input string
string str;
cin >> str;
// Parse each character in the string.
std::locale loc;
for (int i=0; i < str.length(); i++) {
if isdigit (str[i], loc) {
int idx = (int)str[i];
counts[idx]++
total++;
}
// Print results
cout << "The #/digits found in << str << " is:" << total << endl;
// If you wanted, you could also print the total for each digit ...
return 0;
}