Generate non-repeating random number (C + +)

Source: Internet
Author: User

Take the bubble sort method as an example:

1#include <stdio.h>2#include <stdlib.h>3#include <time.h>4 intCreatdata (intArr[],intNintMinintmax)5 {6     intI,j,flag;7Srand (Time (NULL));//creat Data8     if(max-min+1<n)return 0;9      for(i=0; i<n;i++)Ten     { One          Do A         { -Arr[i]= (max-min+1) *rand ()/(rand_max+1)+min; -flag=0; the              for(j=0; j<i;j++) -             { -                 if(arr[i]==Arr[j]) -                 { +flag=1; -                 } +             } A         } at          while(flag); -     } -     return 1; - } - voidBubblesort (intA[],intN) - { in     inti,j,t; -      for(i=0; i<n;i++) to     { +          for(j=n-1; j>i;j--) -         { the             if(a[j-1]>A[j]) *             { $t=a[j-1];Panax Notoginsenga[j-1]=A[j]; -a[j]=T; the             } +         } A         /*printf ("%2d times:", i+1); the For (j=0;j<n;j++) + printf ("%d", a[j]); - printf ("\ n");*/ $     } $ } - intMain () - { the     inti,a[6]; -      for(i=0;i<6; i++)Wuyi     { thea[i]=0; -          Wu     } -     if(!creatdata (A,6,1, -)) About     { $printf"generate random number not successful \ n"); -         return 1; -     } -printf"Original data:"); A      for(i=0;i<6; i++) +printf"%d", A[i]); theprintf"\ n"); -Bubblesort (A,6); $printf"after sorting:"); the      for(i=0;i<6; i++) theprintf"%d", A[i]); theprintf"\ n"); the     return 0;  -}

Generate non-repeating random number (C + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.