Simple number generator with check function to avoid repeating

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Simple number generator with check function to avoid repeating



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;

}





Don't use sizeof on a pointer 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
21 mins ago




sizeof


std::vector





@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: pastebin.com/6QcEmbNY
– Innocent
7 mins ago







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. In this case, fill the array with 0 through 9 and then shuffle the values in the array. The sample code given on the linked documentation page is almost exactly your use-case and easily adaptable.
– user4581301
7 mins ago







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
5 mins ago





@user4581301 I will try it out for sure. Thanks for your answer.
– Innocent
2 mins ago




1 Answer
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.





I'll stand by my above suggestion to just use std::shuffle. No searching, no increasing numbers of retries as the array fills up. Much simpler and faster.
– user4581301
1 min ago


std::shuffle






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

415 Unsupported Media Type while sending json file over REST Template

PHP parse/syntax errors; and how to solve them?