C ++ random number generator

2

The problem is the following I have my code in which it generates 10 random numbers in an array, then shows them and then orders them, what I want is that when I re-execute the "fill" function that is the that generates the numbers an arrangement with new numbers is generated and they are not the same of the previous time.

#include<iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int llenar();
int mostrar();
int array[10];
int burbuja();
int main(){
 llenar();
 mostrar();
 burbuja();
 llenar();
 mostrar();
};

int burbuja(){
  int i,j,aux;

  for(i=0;i<10;i++){
      for(j=0;j<10;j++){
          if(array[j] > array[j+1]){
              aux = array[j];
              array[j] = array[j+1];
              array[j+1] = aux;
          }
      }
  }

  for(i=1;i<11;i++){
      cout<<array[i]<<" ";
  }
  cout<<endl;
}

int mostrar(){
    for(int i=0; i<10; i++){
        cout << array[i] << " ";
   }
cout<<endl;
};

int llenar(){
  array[0]='NULL';

  int num, c;
  srand(time_t());

  for(c = 1; c <= 10; c++)
  {
      array[c-1] = 1 + rand() % (100 - 1);
     /* cout << array[c-1]<< " ";*/
  }

  return 0;
}
    
asked by Ricardo Jesus Jarquin Perez 25.09.2017 в 20:31
source

3 answers

2

Tinenes a few failitos :

int llenar( ) {
  array[0]='NULL';

If it's int array[10] , surely what you want to do is

array[0] = 0;

Changing the above, and also srand( time( NULL ) ); , the numbers already start to come out random.

Apart from that, you declare

int burbuja()

and

int mostrar( )

However, none of them return nothing , and the compiler notifies you.

And the last one:

int llenar( ) {
  array[0] = 0;

  int num, c;

You have another warning that you do not use the variable num .

Edit

Repeat the numbers because, between successive calls to srand( time( NULL ) ) , too little time passes .

To make the effect more noticeable, remove srand( ) of the function llenar( ) and put it at the beginning of main( )

int main( ) {
  srand( time( NULL ) );
  ... resto del código ...
    
answered by 25.09.2017 / 21:10
source
2

If you want " C ++ random number generator " completely forgets the C utilities like rand and the headers of C <stdlib.h> and <time.h> .

Starting with the C ++ 11 standard, the C ++ language offers a complete library of generation of pseudorandom numbers that allows to choose the probability distribution (uniform, Bernoulli, Poisson, normal, discrete, constant, linear ...), the underlying type of the generated value and even the algorithm to be used (minstd, mt19937, ranlux, knuth ...).

From your question I infer that you want a uniform distribution (which is what rand() 1 ) between 1 and 100. With the library of pseudo-random C ++ numbers it can be done like this:

// Este arreglo tiene todos los valores a 0 por defecto. El compilador se encarga de ello.
int array[10]{};

//vv <---- devuelve void, no int.
void llenar(){
    // El random device gestiona las semillas de generacion
    std::random_device rd;
    // Usamos el algoritmo mt19937
    std::mt19937 gen(rd());
    // Obtendremos una distribucion uniforme entre 1 y 100
    std::uniform_int_distribution<> dis(1, 100);

    for (auto &valor : array){
        valor = dis(gen);
    }

    // La funcion no devuelve nada porque no tiene nada que devolver.
}

Every time you call llenar a new std::random_device will be created which it will have a different seed and consequently give different numbers.

1 But its uniformity is destroyed by using the module operation, as it happens in your code.

    
answered by 26.09.2017 в 11:08
0

Try to include the ctime library:

#include <ctime>

And then in the fill function, change:

srand(time_t());

By:

srand(time(NULL));

The srand function defines a seed that changes the pseudorandom sequence generated by rand . Using time (NULL) , the current time value is used as seed, which will make the sequence generating rand different each time the program is executed. Your code is already doing this, but (without testing it), I think that should work this way.

    
answered by 25.09.2017 в 20:50