random () and srand()

Discussion in 'C++' started by free4ziyi@yahoo.com, Oct 19, 2005.

  1. Guest

    Hi,

    My function should takes an array containing the set of all integers in
    the range of [a,b] and shuffles it into random order.

    For example, a = 1, b = 5, n = 3 and the random orders:
    1.[3,4,2,1,5] 2.[2,5,1,4,3] 3.[1,4,2,3,5]

    Then the most similar pair of sequences is 1 and 3, since they have the
    same value in 3 position.

    My problem is should I do to declare the array without knowing the
    array size? The compiler keeps getting error because I declare and
    array like int array[];

    Can anyone help me with this?

    Cheers!

    J
     
    , Oct 19, 2005
    #1
    1. Advertising

  2. Neil Cerutti Guest

    On 2005-10-19, <> wrote:
    > My problem is should I do to declare the array without knowing the
    > array size? The compiler keeps getting error because I declare and
    > array like int array[];
    >
    > Can anyone help me with this?


    std::vector<int> is ready and willing to help you. Also, standing
    over by the bar, is std::shuffle, looking forlorn.

    --
    Neil Cerutti
     
    Neil Cerutti, Oct 19, 2005
    #2
    1. Advertising

  3. Kai-Uwe Bux Guest

    wrote:

    > Hi,
    >
    > My function should takes an array containing the set of all integers in
    > the range of [a,b] and shuffles it into random order.
    >
    > For example, a = 1, b = 5, n = 3 and the random orders:
    > 1.[3,4,2,1,5] 2.[2,5,1,4,3] 3.[1,4,2,3,5]
    >
    > Then the most similar pair of sequences is 1 and 3, since they have the
    > same value in 3 position.


    I do not understand the above description of the problem. What exactly is
    the input (a set of integers, or just the numbers a and b)? What exactly is
    the output (a vector, an array)? And how does n enter the picture? Also,
    what is this notion of similarity that all of a sudden pops up.


    > My problem is should I do to declare the array without knowing the
    > array size? The compiler keeps getting error because I declare and
    > array like int array[];


    Try using std::vector. Those critters known their size.


    > Can anyone help me with this?


    Most certainly. But it will be easier if you show the code you have.



    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Oct 19, 2005
    #3
  4. Neil Cerutti wrote:

    > On 2005-10-19, <> wrote:
    >
    > std::vector<int> is ready and willing to help you. Also, standing
    > over by the bar, is std::shuffle, looking forlorn.
    >


    Also, std::fill_n and boost::count_iterator, for a geeky way to create the
    sequence.

    --
    Sebastian Redl
     
    Sebastian Redl, Oct 19, 2005
    #4
  5. Marcus Kwok Guest

    <> wrote:
    > My function should takes an array containing the set of all integers in
    > the range of [a,b] and shuffles it into random order.
    >
    > For example, a = 1, b = 5, n = 3 and the random orders:
    > 1.[3,4,2,1,5] 2.[2,5,1,4,3] 3.[1,4,2,3,5]
    >
    > Then the most similar pair of sequences is 1 and 3, since they have the
    > same value in 3 position.
    >
    > My problem is should I do to declare the array without knowing the
    > array size? The compiler keeps getting error because I declare and
    > array like int array[];
    >
    > Can anyone help me with this?


    In C++, the size of an array must be a compile-time constant. In C99,
    they added Variable Length Arrays (VLAs), but that has not become
    standard in C++ yet.

    You could try using a std::vector<int> (so you do not have to worry
    about the size), and then have a look at the random_shuffle() function
    in <algorithm>.

    --
    Marcus Kwok
     
    Marcus Kwok, Oct 19, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Intaek LIM
    Replies:
    1
    Views:
    440
    Andreas Kahari
    Oct 31, 2003
  2. Chelong
    Replies:
    11
    Views:
    1,282
    James Kanze
    Jul 10, 2007
  3. globalrev
    Replies:
    4
    Views:
    810
    Gabriel Genellina
    Apr 20, 2008
  4. Arijit Das

    srand versus srandom - srand with random() safe?

    Arijit Das, Oct 17, 2011, in forum: C Programming
    Replies:
    12
    Views:
    3,224
    Nick Keighley
    Oct 18, 2011
  5. VK
    Replies:
    15
    Views:
    1,320
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page