Random-filled vector always returns the same numbers

I am completely new in C and try to fill a vector with random integers and output it afterwards. However, it seems that the vector is always filled with the same number. What do I need to change to really get random payments? There is no limit, the number should only be within the value range of long. Here's my code:

    #include "stdafx.h"
    #include <stdlib.h>
    #include <stdio.h>
    #include <vector>
    #include <time.h>

int main()
{
    long x = 20;
    long y = 12;
    std::vector<long> entries;

    //initialize simple random number generator using current time
    srand(time(NULL));

    for (int step = 0; step < x; step++) {      
        entries.push_back(rand());
        ++entries[step];
    }

    for (std::vector<long>::const_iterator i = entries.begin(); i != entries.end(); ++i) {
        printf("%ld\n", i);
    }       

    return 0;
}

EDIT: I want to solve the issue using plain C not c++!

c

Answers

answered 2 months ago Kaldrr #1

The main problem in your code is in your last loop

for (std::vector<long>::const_iterator i = entries.begin(); i != entries.end(); ++i) {
    printf("%ld\n", i);
}  

You're not printing a value from the vector, but an iterator that's pointing to it

Change the line inside the loop to printf("%ld\n", *i); to fix it.

Better yet, you can use a range-loop to make it even simplier

for(auto& number : entries){
std::cout<<number<<'\n'; //or printf("%ld\n",number), like in your code
}

answered 2 months ago Martin Véronneau #2

If you want your solution to be pure C, you'll need to use arrays instead of vectors.

I'm missing some informations here, but a basic C program that uses srand() and rand() would look like this :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>

#define NB_ENTRIES (20)
#define RANDOM_MAX_RANGE (50) // you should update that

// Don't use srand(time(NULL)) because if you start many time this
// program too quickly, you'll have the same values in all instances.
void Randomize(void)
{
  struct timeval time; 
  gettimeofday(&time,NULL);
  srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
}

// Returns a value between 0 and toNumber-1
// Thanks to Bathsheba for pointing out an error with my first version.
int RandomInt(int toNumber)
{
  return (int)(rand()/(RAND_MAX + 1.0) * toNumber);
}

// This better but slower version of RandomInt() will ensure
// an even distribution of random values if toNumber is
// greater than around three quarter of RAND_MAX
// NOTE : toNumber must be between 0 and RAND_MAX.
int BetterRandomInt(int toNumber)
{
  const int randomSize = RAND_MAX / toNumber;
  int returnValue;
  do 
  {
    returnValue = rand() / randomSize;
  }
  while (returnValue >= toNumber);

  return returnValue;
}

int main(void)
{
  int entries[NB_ENTRIES];
  int step;

  Randomize();

  for (step = 0; step < NB_ENTRIES; step++)
  {
    entries[step] = RandomInt(RANDOM_MAX_RANGE);
    // entries[step] = BetterRandomInt(RANDOM_MAX_RANGE);
  }

  for (step = 0; step < NB_ENTRIES; step++)
  {
    printf("%d\n", entries[step]);
  }       

  return 0;
}

Note that I'm using a fixed number of entries. You can make your array dynamic by using pointers and malloc(). Just comment if you need that, but I felt it was out of scope of the question.

comments powered by Disqus