mercredi 6 juillet 2016

Non-repeating random numbers

I'm working on a synonym puzzle, it gives you a word and wants you to find the synonym of it given the lenght of the word. Everything works fine, but it happens on an ordered sequence; words do not appear randomly. I need knumber of non-repeating random numbers for that. Here's my code:

#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
    const int k=4;
    string word[k]={"furious","tiny","untrue", "humorous", "harm"};
    string nword[k]={"angry","small","false", "funny", "damage"};
    string j;
    int point, p = 0;
    int ctr=0;

    srand(time(NULL));
    int randNum = (rand() % k) + 0;


    for(int i=0; i<k; i++)
    {
        cout << nword[i] << "\n";
        cout << "The length of the word: " << word[i].length() << "\n";
        cin>>j;



        ctr++;
        if(j==word[i])
        {
            cout<<"Correct! Score: " << i+1 << " point." << "\n\n";


        }
        else
        {
            cout<<"Wrong"<<endl;
        }

    }

    return 0;
}

As you can see, the variable randNum holds the value of the random number from 0 to k, (k is 4, combined with 0, I got 5 words). In for loop, when I set the nword and word like nword[randNum], and word[randNum], the result leaves a lot to be desired. First, I think there's no sync for the two (nword and word). It will apply different random numbers for the two (I might be wrong) and the second, it will be repetitive. As seen, the execution is score-based and completable, so I need non repeating questions until it reaches to k.




Aucun commentaire:

Enregistrer un commentaire