-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.cpp
More file actions
53 lines (48 loc) · 1.21 KB
/
random.cpp
File metadata and controls
53 lines (48 loc) · 1.21 KB
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
47
48
49
50
51
52
53
/*Generar numeros aleatorios
Nombre: Erik Villarreal
Fecha: 12 de diciembre 2022
*/
#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<sstream>
using namespace std;
void escribir(int dato, int numeros);
ofstream archivo;
int main(){
int min = 0, max = 0, numeros;
cout << "***Ingrese el rango***"<<endl;
cout << "MIN: ";
cin >> min;
cout << "MAX: ";
cin >> max;
cout << "Cantidad de numeros: ";
cin >> numeros;
int random;
string nombre_archivo = "random_numbers_";
stringstream ss;
ss << numeros;
string aux;
ss >> aux;
nombre_archivo = nombre_archivo + aux;
nombre_archivo = nombre_archivo + ".dat";
archivo.open(nombre_archivo.c_str(), ios::out);
for(int i=0; i<numeros; i++){
random = min + rand() % (max - min);
cout <<"["<<i+1<<"]: "<< random << endl;
escribir(random, numeros);
if (i<numeros-1)
archivo<<endl;
}
archivo.close();
return 0;
}
void escribir(int dato, int numeros){
if (archivo.fail()){
cout << "¡No se pudo crear el archivo!" << endl;
exit(1);
}
else{
archivo << dato;
}
}