random character generate

im writing a program to help me understand on rand fucntion i dont get how to use it with a file that has a bunch of characters i think i would have to do it in a loop so it could be random and set srand (time(NULL));

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <string.h>
#include <cstdlib>
using namespace std;
struct characters
{
    char setOfCharacter;
};

int main()
{
     characters charInfo[4];
     ifstream inFile;
     inFile.open("character.txt");
     while(!inFile)
     {
         cout << "error" << endl;
         return 0;

     }
     for(int i =0; i<4; i++)
     {
         inFile >> charInfo[i].setOfCharacter;
     }
     inFile.close();

     


    return 0;
}

here is my file
4
ABC
Abc
AbC
aBC
i got it random generate but i want to grab a just one line not all the lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <string.h>
#include <cstdlib>
using namespace std;
struct characters
{
    char setOfCharacter1;
    char setOfCharacter2;
    char setofCharacter3;
};

int main()
{
    int totalCharacter=4;
     characters charInfo[totalCharacter];
     srand(time(NULL));
     ifstream inFile;
     inFile.open("character.txt");
     while(!inFile)
     {
         cout << "error" << endl;
         return 0;

     }
     for(int i =0; i<totalCharacter; i++)
     {
         inFile >> charInfo[i].setOfCharacter1;
         inFile >> charInfo[i].setOfCharacter2;
         inFile >> charInfo[i].setofCharacter3;
     }
     inFile.close();

      for(int i =0; i<totalCharacter; i++)
      {
          int x =rand() %4;
          cout << charInfo[x].setOfCharacter1 << charInfo[x].setOfCharacter2 << charInfo[x].setofCharacter3 <<endl;
      }





Last edited on
understand on rand fucntion

by all means do so but the most up-to-date way of generating random numbers in C++ is using the <random> library - search this forum for several examples last few weeks
grab a just one line

use std::getline() to read the file by line into std::string, save the lines in a std::vector<std::string>, std::random_shuffle the vector and pick-off any particular element like vec[0], etc
i dont understand what you mean

"use std::getline() to read the file by line into std::string, save the lines in a std::vector<std::string>, std::random_shuffle the vector and pick-off any particular element like vec[0], etc "

i want to use rand() not the library
the things is i have character not numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
std::string random_line_from( std::string file_name )
{
    std::string selected_line ; // the randomly chosen line

    std::ifstream file(file_name) ; // open the file for reading
    std::string current_line ; // the current line read from the file
    std::size_t line_number = 1 ;

    while( std::getline( file, current_line ) ) // for each line in the file
    {
        // the current line becomes the selected line with a probability of 1 / line_number
        // the first line is selected with a probability of 1
        // the second line replaces  the selected line with a probability of 1/2
        //     so now, each line so far being the selected line has a probability of 1/2
        // the third line replaces  the selected line with a probability of 1/3
        //     so now, each of the three lines so far being the selected line has a probability of 1/3
        // etc.
        if( ( std::rand() % line_number ) == 0 ) selected_line = current_line ;
        ++line_number ;
    }

    return selected_line ;
}

http://coliru.stacked-crooked.com/a/8d76660426f8b27e
i have character not numbers

who said anything about numbers?
i want to use rand()

why when it's not the most accurate option available unless your professor insists
i just want to be able to just to use rand() srand(time(null))
thank you for the example @jlborgers
since it a string could i do it with a character
Last edited on
If C-style arrays must be used:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// assumes that no line in the file is longer than 4095 characters
const std::size_t MAX_LINE_SZ = 4096 ; // +1 for the null-character at the end

char* random_line_from( const char* file_name, char selected_line[MAX_LINE_SZ] )
{
    selected_line[0] = 0 ; // initialise to a null string

    std::ifstream file(file_name) ; // open the file for reading
    char current_line[MAX_LINE_SZ] ; // the current line read from the file
    std::size_t line_number = 1 ;
    
    while( file.getline( current_line, MAX_LINE_SZ ) ) // for each line in the file
    {
        // the current line becomes the selected line with a probability of 1 / line_number
        // the first line is selected with a probability of 1
        // the second line replaces  the selected line with a probability of 1/2
        //     so now, each line so far being the selected line has a probability of 1/2
        // the third line replaces  the selected line with a probability of 1/3
        //     so now, each of the three lines so far being the selected line has a probability of 1/3
        // etc.
        if( ( std::rand() % line_number ) == 0 ) std::strcpy( selected_line, current_line ) ;
        ++line_number ;
    }

    return selected_line ;
}

http://coliru.stacked-crooked.com/a/4574c133474cd9bc
Topic archived. No new replies allowed.