1

I am trying to write simple function to check if randomly generated value is already in array. I want to simply reach a number generator without repeating. Unluckly my code does not work. I mean it still generates me duplicated values. Could anyone tell me what I am doing wrong?

Checks if value exists in array:

bool valueExistsInArray(int* array, int value) {

if ((sizeof(array) / sizeof(*array)) <= 0) 
    return false; 

int i = 0;

do {
    if (array[i] == value)
        return true;
    i++;
} while (i < (sizeof(array) / sizeof(*array)));

return false;

}

Generating random values from specified range:

int generateRandomValue(int from, int to) {  

return (std::rand() % (to-from+1)) + from;

}

Main function:

int main()
{
srand((unsigned int)time(NULL));

int array[10];
int i = 0;

do {

    int value = generateRandomValue(1, 10);

    if (!valueExistsInArray(array, value)) {
        array[i] = value;
        i++;
    }

} while (i < (sizeof(array) / sizeof(*array)));

for (int i = 0; i < (sizeof(array) / sizeof(*array)); i++)  
    std::cout << "Index: " << i << " Value: " << array[i] << "\n";


_getch();

return 0;

}
Innocent
  • 11
  • 1
  • [Don't use `sizeof` on a pointer array](https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array). That won't work. Either pass the array's size as a parameter or use a `std::vector`, which you would probably want to pass by reference. – alter_igel Jul 25 '18 at 20:56
  • @alterigel Thanks, it seems the problem was in sizeof, I fixed it as you said. I'm a begginer and started learning programming like 2 days ago so I still know nothing about vectors. I will read about it for sure. Here is fixed code, maybe it will be helpful for someone: https://pastebin.com/6QcEmbNY – Innocent Jul 25 '18 at 21:09
  • Side note: An easier way to guarantee uniqueness when the size of the array matches the number possible values (each value is in the array exactly once) is [the Fisher-Yates Shuffle](https://en.wikipedia.org/wiki/Fisher–Yates_shuffle). In this case, fill the array with 0 through 9 and then [shuffle the values in the array](https://en.cppreference.com/w/cpp/algorithm/random_shuffle). The sample code given on the linked documentation page is almost exactly your use-case and easily adaptable. – user4581301 Jul 25 '18 at 21:09
  • Feel free to take your fixed code, add on an explanation of what you changed and why, and self answer your question. Not only does it make the answer to the question obvious to any future programmer finds this while searching, it may earn you some good karma. – user4581301 Jul 25 '18 at 21:11
  • @user4581301 I will try it out for sure. Thanks for your answer. – Innocent Jul 25 '18 at 21:14
  • @Innocent if you're just beginning to learn C++, it seems like you're being taught from a source that barely knows the difference from C. Modern C++ is markedly different from C and follows a different design mentality. If you want to pursue C++, I would suggest learning instead from a [recommended C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – alter_igel Jul 25 '18 at 21:20

1 Answers1

1

simple example how to do it using modern c++

#include <random>
#include <set>
#include <iostream>


int main()
{
    const uint32_t minValue = 0;
    const uint32_t maxValue = 1000000;

    const size_t numValues = 100;

    std::random_device rd;
    std::uniform_int_distribution<int> dist(minValue, maxValue);

    std::set<uint32_t> myNumbers;

    while (myNumbers.size() < numValues)
    {
        myNumbers.insert(dist(rd));
    }

    for (const auto& val : myNumbers)
    {
        std::cout << val << "\n";
    }

    return 0;
}

random_device and uniform distribution give you proper random values.

the "set" takes care of filtering duplicates.

if you just want to shuffle a vector of e.g. numbers 0 to 100 use std::shuffle, example:

#include "stdafx.h"

#include <algorithm>
#include <random>
#include <vector>
#include <iostream>


int main()
{
    std::random_device rd;
    std::mt19937 g(rd());

    std::vector<uint32_t> myNumbers(100);
    std::iota(myNumbers.begin(), myNumbers.end(), 0);
    std::shuffle(myNumbers.begin(), myNumbers.end(), g);

    for (const auto& val : myNumbers)
    {
        std::cout << val << "\n";
    }

    return 0;
}
skeller
  • 1,151
  • 6
  • 6