Sep 5, 2009

Random Integer Generator without repeatation

/*
* RandomIntegerGenerator.cpp
*
* Created on: Sep 4, 2009
* Author: mdreza
*/
#include < iostream >
#include < fstream >
#include< vector >
#include< cstdlib >
using namespace std;
vector < int > random_vector;

void initialization(int max_size) {

for (int i=0; i random_vector.push_back(i);
}

}



int main() {

int N = 5;
int counter = N;
srand(time(0));
ofstream outputFile("random.txt");

// This produces random number with repeated values.
// int number[10];
// for(int i = 0, j = N ; i <= N ; i++,j--){
// number[i] = rand()%j;
// outputFile<// }

initialization(N);

while (counter >= 1) {
int random_number = rand() % counter;

// swap the random_vector[random_number] and random_vector[counter]

int temp = random_vector[random_number];
random_vector[random_number] = random_vector[counter-1];
random_vector[counter-1] = temp;

counter = counter - 1;

}

for (int i=0; i outputFile << random_vector[i] << endl;
}

outputFile.close();

return 10;

}

Reference help

3 comments:

Anonymous said...

Well Alim vai......
I needed this thing last month,
but it will be effective in my thesis work......
Thanks

Md. Alimoor Reza said...

Thanks Mahmud.
Ageo koyek bar kaje lagse.
Bar bar same jinish code kora pain.
Save kore rakha shubidha jonok.

It's my pleasure if it helps :)

rahatibnrafiq- said...

josh jinis alim vai..apnar ekhn thekey cpy paste korbo:P:P..ei jinista krte krte tired...

Cat

Ragdoll Cat         Persian Cat     Finish Cat