no duplication random, (stl random_shuffle example source code)

sometimes, we need no duplication random sequence data.
simple method is using shuffle random in stl.

refer to this source code.

#include < stdio.h>
#include < vector>
#include < algorithm>
#include < cstdlib>
#include < ctime>
#include < iostream>
using namespace std;


void main()
{
 //set size and initialize
 vector< int > A(10);
 for(int i=0; i< A.size(); ++i)
  A[i] = i;

 //confirm
 printf("----origin data \n");
 for(int i=0; i< A.size(); ++i)
  printf("[%d] - %d \n", i, A[i] );
 printf("----\n");

 //random 
 srand( unsigned (time(0) ) );
 random_shuffle( A.begin(), A.end() );

 //confirm
 printf("---- After shuffle \n");
 for(int i=0; i< A.size(); ++i)
  printf("[%d] - %d \n", i, A[i] );
 printf("----\n");
}


..

Comments

Popular posts from this blog

OpenCV Stitching example (Stitcher class, Panorama)

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Optical Flow sample source code using OpenCV

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

(OpenCV Study) calcOpticalFlowFarneback example source code ( dense optical flow )

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)