Velocity Reviews > C++ > random () and srand()

# random () and srand()

free4ziyi@yahoo.com
Guest
Posts: n/a

 10-19-2005
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

Neil Cerutti
Guest
Posts: n/a

 10-19-2005
On 2005-10-19, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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?

over by the bar, is std::shuffle, looking forlorn.

--
Neil Cerutti

Kai-Uwe Bux
Guest
Posts: n/a

 10-19-2005
(E-Mail Removed) 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

Sebastian Redl
Guest
Posts: n/a

 10-19-2005
Neil Cerutti wrote:

> On 2005-10-19, (E-Mail Removed) <(E-Mail Removed)> wrote:
>
> 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

Marcus Kwok
Guest
Posts: n/a

 10-19-2005
(E-Mail Removed) <(E-Mail Removed)> 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