random word

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: random word

Post by albinopapa » April 20th, 2015, 12:08 am

chili wrote:Simple google search yielded this:
http://www.mieliestronk.com/corncob_lowercase.txt
Use this as your starting point. Call getline in a loop while counting so you know how many words are in the list. Rewind the file. Call rand() % listCount. Run loop with getline again the number of times that is returned from the rand function and then display the word. No need for SQL databases, local or remote, no need for internet stuff, no need for vectors, just simple text file access.

To make lookup faster and file size smaller, you could create a struct for records,

Code: Select all

struct Record
{
     unsigned int index;
     const char *text[20];
}
As you read in your list as a text file, you record the count in the index and the word in text, then write to a binary file. That way, when you call rand() % listCount, you can calculate the exact position of the record by multiplying the result by 24, simple binary file access.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: random word

Post by Clodi » April 23rd, 2015, 8:34 am

Again,
Thank you all for the useful insights and explanations. Back to work :D

User avatar
bobloblaw
Posts: 100
Joined: April 15th, 2013, 3:05 am
Location: Earth, USA, California, SF Bay Area
Contact:

Re: random word

Post by bobloblaw » May 4th, 2015, 2:33 am

This is a QUICK execution of a program that does something similar to what you were asking for. It's a console app but can easily be modified to work within chili's framework. If anything doesn't make sense, let me know and I'll try and explain it. I uploaded a .txt file to be loaded into the program. If the file is going to be rather large then I would suggest a .dat file type be used but the code would need altering to use .dat
~cheers

Code: Select all

#include <iostream>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <ctime>

using namespace std;

//function prototypes
void displayWord( vector<string>, vector<bool> &, unsigned );
unsigned GetRandomNum( int, int );

int main()
{
	// Create a vector to hold words list.
	vector<string> words;

	// Create file stream object.
	ifstream inputFile;

	// Open the file.
	inputFile.open( "WordList.txt" );

	if( !inputFile )
    {
        cout << "ERROR: Specified file was not found!" << endl;
    }
    else
    {
        string str;

        // Load words into vector in memory.
        while( inputFile >> str )
        {
            words.push_back( str );
        }

        // Close the file.
        inputFile.close();

        int sizeOfVect = words.size();

        // Create a vector to hold if word has been displayed.
        vector<bool> wordsAlreadyDisplayed( sizeOfVect );

        char again;

        do
        {
            unsigned randomInt = GetRandomNum( 1, sizeOfVect );

            displayWord( words, wordsAlreadyDisplayed, randomInt );
            cout << "Would you like to continue? ( Y or N ): ";
            cin >> again;
        } while( again == 'y' || again == 'Y' );
    }

	return 0;
}

// Function to return a random int value between user defined range.
unsigned GetRandomNum( int x, int y )
{
    // Set up constants limitations of dice
    int minValue = x;
    int maxValue = y;

    // Vars to hold rand value
    unsigned randNum = 0;

    // Get the system time to use with random num generator.
    unsigned seed = time(0);

    // Seed the random num gen.
    srand(seed);

    randNum = ( rand() % ( maxValue - minValue + 1 ) ) + minValue;

    return randNum;
}

void displayWord( vector<string> vectNames, vector<bool> &wordsAlreadyDisplayed, unsigned randomInt )
{
    // if word displayed equals false.
	if( !wordsAlreadyDisplayed[ randomInt ] )
	{
		// Display the word.
		cout << vectNames[ randomInt ] << endl;

		wordsAlreadyDisplayed[ randomInt ] = true;
	}
}
Attachments
WordList.txt
(245 Bytes) Downloaded 109 times
Here for your Artistic needs, Pixel or Vector I love it all. Check it out - Click Me

Post Reply