Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-05-2007, 09:42 PM
clankman's Avatar   
clankman clankman is offline
Newbie
 
Join Date: May 2007
Location: USA
Posts: 20
Rep Power: 6
clankman is on a distinguished road
Send a message via AIM to clankman
Default How to get random words

I am making a hangman game, and I need to know how to get a word (from a list of words) that is randomly selected.

I also need to know where to put/make the list of words.
__________________
Never be bullied into silence. Never allow yourself to be made a victim. Accept no one's definition of your life; Define yourself. -Harvey Fierstein
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 08-06-2007, 04:43 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

First you need to generate the number, which is the hardest part of what you're seeking. Next, it would be the easiest to use an vector, which also have good facilities for adding new words.

I'm unable to make a complete example for you at the moment, but if I get the time, I can make a working example - but first in next week, sorry.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-06-2007, 11:48 PM
NoName NoName is offline
Newbie
 
Join Date: Aug 2007
Posts: 6
Rep Power: 0
NoName is on a distinguished road
Default

One way to do it is have the list stored in a basic array something like this (i am using the string class but you could do this with a 2 dimensional array but thats a pain so if you do it like this you need to #include<string>)

Code:
string words[] = {"cat","dog","door","moon"};
then to pick one at random (you need to #include<ctime>)
Code:
//This is used to seed the random sequence generated by rand()
	srand(time(NULL));
	//Use the %4 to keep the random number between 0-4
	cout << words[rand()%4] << endl;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-07-2007, 04:57 PM
clankman's Avatar   
clankman clankman is offline
Newbie
 
Join Date: May 2007
Location: USA
Posts: 20
Rep Power: 6
clankman is on a distinguished road
Send a message via AIM to clankman
Default

Your code seemed to work (not 100% sure if it did, but didn't get any errors). Just a question or two about it.

Does putting %4 in there make it so the biggest a word can be is four letters?

Also, is there a limit to the amount of words I can enter in the string?
__________________
Never be bullied into silence. Never allow yourself to be made a victim. Accept no one's definition of your life; Define yourself. -Harvey Fierstein
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-08-2007, 12:40 AM
NoName NoName is offline
Newbie
 
Join Date: Aug 2007
Posts: 6
Rep Power: 0
NoName is on a distinguished road
Default

the %4 all it does is make it so the random number is only going to be 0,1,2, or 3 it has nothing to do with the length of the word

Last edited by NoName; 08-11-2007 at 01:51 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 08-11-2007, 01:45 AM
NoName NoName is offline
Newbie
 
Join Date: Aug 2007
Posts: 6
Rep Power: 0
NoName is on a distinguished road
Default

Here is a full example that will load the words from a file and then keep them in a vector

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>

using std::cout;
using std::fstream;
using std::string;
using std::vector;

bool loadWords(char *,vector<string> &);

int main()
{
	srand(time(NULL));

	vector<string> wordList;
	if(loadWords("C:\\wordFile.txt",wordList))
	{
		cout << "5 Random words from the file\n";
		for(int i=0; i<5; i++)
			cout << wordList.at(rand() % wordList.size()) << '\n';
	}
	else
		cout << "There was an error opening the file\n";

	return 0;
}

bool loadWords(char * fileName,vector<string> & words)
{
	fstream file(fileName,std::ios::in);
	char buffer[30];

	if(file.is_open())
	{
		words.clear();
		while(!file.eof())
		{
			file.getline(buffer,30);
			words.push_back(buffer);
		}
		file.close();
		return true;
	}
	else
		return false;

}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-11-2007, 04:00 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

As I said, I will come with a full example.
Code:
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <cstdlib>
#include <ctime>

void Wait(int iTime)
{
	clock_t End;
	End = clock () + iTime;
	while(clock() < End);
}

void AddWord(std::vector<std::string> &vList, std::string sWord)
{
	std::cout << " - Adding ... \"" << sWord << "\"" << std::endl;
	vList.push_back(sWord);
}

std::string GetWord(std::vector<std::string> vList, unsigned int iIndex)
{
	return vList.at(iIndex);
}

int GetRandom(int iMax)
{
	int iRandom;
	
	Wait(100);
	
	srand(static_cast<unsigned int>(time(NULL) * clock()));
	iRandom = (rand() / (RAND_MAX / iMax + 1));
	
	return iRandom;
}

int main()
{
	int iSize;
	int iRandom;
	std::string sWord;
	std::vector<std::string> vWords;
	
	std::cout << "Adding 10 words to the list" << std::endl;
	AddWord(vWords, "Book");
	AddWord(vWords, "Wall");
	AddWord(vWords, "Computer");
	AddWord(vWords, "Calculator");
	AddWord(vWords, "Television");
	AddWord(vWords, "Bed");
	AddWord(vWords, "Flower");
	AddWord(vWords, "Chair");
	AddWord(vWords, "Table");
	AddWord(vWords, "Cup");
	
	assert(vWords.size() == 10);
	
	std::cout << std::endl;	
	std::cout << "Generating 5 random words from the list" << std::endl;
	for(unsigned int iIndex = 0; iIndex < 5; iIndex++)
	{
		iSize   = vWords.size();
		iRandom = GetRandom(iSize);
		sWord   = GetWord(vWords, iRandom);
		
		std::cout << " - Returned ... \"" << sWord << "\"" << std::endl;
	}
	
	return 0;
}
Note the GetRandom and Wait-functions. In GetRandom I'm using the time to get a random number, but most computers nowadays are so fast that the application would run too fast, so time couldn't change from call to call. So we need a Wait-function which does it for us. I'm using std::vector and std::string which is the easiest to work with.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating Random Numbers with PHP Paradine PHP Tutorials 4 08-27-2007 08:09 PM
Return random numbers without duplicates Paradine PHP Tutorials 0 08-26-2007 03:07 PM
How do I program a random generator? eddiewillers General Programming 4 07-24-2007 03:26 PM
Dictonary Program programmer 101 Java Help 9 07-01-2007 02:39 PM
Code:PHP Random Functions John PHP Tutorials 2 12-03-2006 11:04 AM


All times are GMT -5. The time now is 02:18 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads