Search code examples
c#phprandomwebclient

Getting random words


I am looking to use randomly selected English words in my current C# project. One way I have thought of doing this is to access the following website and retrieve a randomly generated word:

http://www.wordgenerator.net/random-word-generator.php

However, I don't really know how to do this. So far I have tried the following code (which doesn't work as intended):

string downloadedString;
WebClient client;

client = new WebClient();
downloadedString = client.DownloadString("http://www.wordgenerator.net/random-word-generator.php#rname");

Could someone please show me how to get a randomly generated word/words from the website given. Also, if someone knows a better method for generating random words I would like to hear that too.


Solution

  • Using the same site with a different URL, this will give you a different set of 51 nouns each time:

    client = new WebClient();
    downloadedString = client.DownloadString("http://www.wordgenerator.net/application/p.php?id=nouns&type=50&spaceflag=false");            
    string[] randomWords = downloadedString.Split(',');
    

    You can consume this API which will be a better idea.

    http://randomword.setgetgo.com/

    Screen scraping is a bad idea as screens can change breaking your parsing implementation. Consuming a REST API is more reliable as it should be less prone to change.

    You can consume the above API like this:

    client = new WebClient();
    downloadedString = client.DownloadString("http://randomword.setgetgo.com/get.php");            
    string randomWord = downloadedString;