0
RNG of arrays but some have the same value and doesn't output same number twice
I have an array of integers and dont want the number to be pulled twice with some of the arrays being the same value how do I go about this. This is the code and was my thought on how to do it but it doesn't work. I will still come across an output that shows the same number twice. So what I'm trying to do is add arrays with the same value to bring possibilities up for pulling that value but not pulling it twice within the same output.
5 odpowiedzi
0
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
const int array_size = 25;
srand(time(0));
int nums[array_size] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,3,12,8,10,5};
nums[21] = nums[2];
nums[22] = nums[11];
nums[23] = nums[7];
nums[24] = nums[9];
nums[25] = nums[4];
for (int i = 0; i < 5; i++)
{
int index = rand() % array_size;
int temp = nums[i];
nums[i] = nums[index];
nums[index] = temp;
if (nums[i] == nums[index])
nums[i] = temp;
}
for (int i = 0; i < 5; i++)
{
cout << nums[i] << endl;
}
return 0;
}
0
I will try what you saying ~swim~ I just started learning c++ and coding all together 2 weeks ago so see any example code will help
0
~ swim ~
I'm not sure how to check for the value b4 it outputs the value