Thread: Random picks

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    34

    Random picks

    Hi,
    I am looking for some code that will pick a random name from a text file of names and dislplay the name on the screen. This seems easy enough logically, however, I'm not a programer. Is there someplace or somebody that can direct me to either learn how this is done or a place that has sample code for this?

    Thanks for any information!
    MCorn

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    There are many ways to do this. Probably the most straightforward way is to read all the names from the file into an array or vector in your program. Then pick a random number that is less than the size of the array/vector. Use that random number as the index of an element of the array/vector to access it/display it to the screen/whatever.

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by elad
    There are many ways to do this. Probably the most straightforward way is to read all the names from the file into an array or vector in your program. Then pick a random number that is less than the size of the array/vector. Use that random number as the index of an element of the array/vector to access it/display it to the screen/whatever.
    as a bit of an optimization here, you could generate the random number first if you know how many names there are, then just read up to the nth name and ignore the rest. This of course rely's on knowing the number of names before hand.

  4. #4
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<ctime>
    #include<cstdio>
    int main()
    {
    fstream readfile;
    int num=0,max=0;
    char a;
    string b;
    readfile.open("test.txt",fstream::in);
    srand((unsigned int)time(0));
    while(readfile>>b)
    {
    max++;
    }
    readfile.close();
    cout<<"press 1 to show name,else to exit"<<endl;
    cin>>a;
    while(a='1')
    {
    readfile.open("test.txt",fstream::in);
    num=rand()%max;
    while(num>=0)
    {
    readfile>>(b);
    num--;
    }
    cout<<b<<endl;
    cout<<"press 1 to show name,else to exit"<<endl;
    cin>>a;
    cin.ignore();
    readfile.close();
    }
    return 0;
    }
    it needs you to write one name per line in .txt file

    i write this in hurry, i believe there is some better way....

    blow me ... ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM