Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
Neil Cerutti
Guest
Posts: n/a
 
      10-19-2005
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
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-19-2005
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
 
Reply With Quote
 
Sebastian Redl
Guest
Posts: n/a
 
      10-19-2005
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
 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      10-19-2005
<> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
Random float between 0 and 1, then a second random float between 0and 1 - x sintral C++ 9 12-07-2008 02:23 AM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
problem GD and my package Random->Random::new john.swilting Perl Misc 13 08-09-2007 05:09 AM
Random "The IListSource does not contain any datasources" and more (Crashing a live site at random, twice a week or so) Lars-Erik Aabech ASP .Net 8 04-28-2005 07:52 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57