lundi 22 juin 2015

C++ & Qt: Random string from an array area

In my small Qt application, I want to pick a random string out of an array after I clicked on a button. I've read many threads but nothing works for me.

So in my slot there's an array with several strings in it. I also implemented <string>, <time.h> and srand.

#include "smashrandom.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>

SmashRandom::SmashRandom(QWidget *parent)
    : QWidget(parent)
{
    // shortened version
    connect(button, SIGNAL(clicked()), this, SLOT(Randomizer()));
}

void SmashRandom::Randomizer()
{

    srand((unsigned int)time(NULL));

    std::string characters[6] = {"Mario", "Luigi", "Peach", "Yoshi", "Pac Man", "Sonic"};
}

But how can I pick a random string out of my array "characters"? Normally I use rand()% for int or double arrays, but in this case I don't know how to use it for random strings.

In addition to that, it is possible to select a random string from an area inside the array? For example I just want want a random string from Mario to Yoshi so Pac Man and Sonic can't even show up?

I hope you can understand my problem and thanks in advance. :)




Aucun commentaire:

Enregistrer un commentaire