Results 1 to 6 of 6

Thread: How can I use the random_shuffle() in CPP programming

  1. #1
    Join Date
    Dec 2009
    Posts
    31

    How can I use the random_shuffle() in CPP programming

    Hello, I am the student of the MSC-IT. I had learned the C++ language programming in first year of the graduation. I could know about the in-built functions that can be present in the header files of the libraries of the C++ language. I could must have to submit the assignment on the random_shuffle() function. So, I want to know about the random_shuffle() function of the C++ programming language. I want to know how can I use the random_shuffle() function in C++ programming. Can anyone knows about the random_shuffle() functions of C++ language.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How can I use the random_shuffle() in CPP programming

    Hello, Everybody. According to me the random_shuffle() function can be used in the C++ language to randomly re-arrange the elements in the given specified range [strt,nd). The behavior of the random_shuffle() function template can be similar to the following as :
    Code:
    template <class RndmAcsItrtr, class RndmNmbrGnrtr>
      void random_shuffle ( RndmAcsItrtr fst, RndmAcsItrtr lst,
                            RndmNmbrGnrtr& rnd )
    {
      iterator_traits<RndmAcsItrtr>::difference_type i, m;
      m = (lst-fst);
      for (i=2; i<m; ++i) swap (first[i],first[rand(i)]);
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How can I use the random_shuffle() in CPP programming

    The following program can demonstrates you that how to use the random_shuffle() function in the programming of the C++ language while doing the coding using the random_shuffle() function as shown below :
    Code:
    include <iostream>
    include <algorithm>
    include <cstdlib>
    include <ctime>
    int Rndm(int m)
    {
        return rnd() % m ;
    }
    int main()
    {
            std::srnd(std::time(0));
           int Nmbrs[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            std::random_shuffle(Nmbrs, Nmbrs + 10, Rndm);
            for(int i=0;i<10;++i)
              std::cout<<Nmbrs[i]<<"  ";
            return 0;
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How can I use the random_shuffle() in CPP programming

    Hi, The random_shuffle() function can be used in the C++ language to rearrange an elements randomly in the given specified range. The random_shuffle() function can be the member of the <algorithm> header files. The following can be the syntax of the random_shuffle() function in the C++ language :
    Code:
    template <class RndmAcsItrtr>
      void random_shuffle ( RndmAcsItrtr fst, RndmAcsItrtr lst );

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How can I use the random_shuffle() in CPP programming

    The random_shuffle() function can randomly re-arranges the elements in the given specified range [strt,nd). If a random number generator function object can be supplied, it shall be used instead of an internal random number generator. The “internal” random number generator can not be necessarily the one that can be an accessed with the rnd() function and can be seeded with srnd() function; that can implementation defined.

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    Re: How can I use the random_shuffle() in CPP programming

    Program :
    #include <vector>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    int main()
    {
    vector<char> wc;
    wc.pshbck('a');
    wc.pshbck('b');
    wc.pshbck('c');
    wc.pshbck('d');
    cout<<wc[0]<<wc[1]<<wc[2]<<wc[3]<<endl;
    random_shuffle(wc.begin(), wc.end());
    cout<<wc[0]<<wc[1]<<wc[2]<<wc[3]<<endl;
    }
    The above program can help's you to know about the random_shuffle() function of the C++ language.

Similar Threads

  1. What does DOM mean in programming?
    By Gadin in forum Software Development
    Replies: 5
    Last Post: 11-03-2010, 05:33 PM
  2. Xml with C# in .NET programming
    By Remedy in forum Software Development
    Replies: 4
    Last Post: 03-03-2010, 08:10 PM
  3. Socket programming: Is any new Programming Language?
    By Kushan in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 11:13 AM
  4. Programming - Mat
    By garfield1 in forum Software Development
    Replies: 7
    Last Post: 02-02-2009, 11:09 PM
  5. Replies: 3
    Last Post: 13-12-2008, 01:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,286,853.10485 seconds with 17 queries