randomly choosing from a outside source??

This is a discussion on randomly choosing from a outside source?? within the C++ Programming forums, part of the General Programming Boards category; I know my title sounds a little weird sorry. This is what I want to do. I want to randomly ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    randomly choosing from a outside source??

    I know my title sounds a little weird sorry. This is what I want to do. I want to randomly pick a word from a txt file. The words are seperated by new lines or "\n" and I want the program to randomly pick one of the many I have included. It is for a hangman style game inside my rpg I am working on. I have no idea how to start this. I am not asking for u to do it for me I just want a shove in the right direction. I will gladly do all the coding myself but I can't figure out how to go about this?? thank you all fot the help.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    One option is to create a random number and read the file up to that number, the word that you read next is the random word. This can be touchy if you don't know how many words are in the file though, you may have a situation where you read up to EOF and would have to rewind the file to continue.

    There are a number of ways to go about this though.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    208

    Thanx

    I sort of understand what you mean. but if i create a random number won't it sometimes end up in the middle of the word?
    Could I possibly number the lines? I dunno thanx again.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but if i create a random number won't it sometimes end up in the middle of the word?
    No, you create the random number and then read individual words, each time incrementing a counter. When the counter reaches the random number, you're just before the word you want.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug into Source
    By lehe in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2009, 10:45 AM
  2. Open Source and Security
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 06-17-2008, 02:23 AM
  3. Open Source Licenses
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 10-10-2006, 09:53 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21