i have to do this assigment by the next monday...;)
i need to develop a test bank so i have to generate :
1-100 arrays of size 10 with randomly distinct numbers from (0-9)
2-100 arrays of size 50 with randomly distinct numbers from (0-49)
3-100 arrays of size 100 with randomly distinct numbers from (0-99)
4-100 arrays of size 200 with randomly distinct numbers from (0-199)
can u please help in the generation of these arrays..!
i used this code for the generation of the 100 arrays of size 10 but still i have problems in making the numbers distinct and limiting them in the required range for each size..can u help with that please:-| !
#include<iostream.h>
#include<cstdlib>
void main()
{ for(int i=0;i<=100;i++)
{
int a[10];
for (int j=0;j<=10;j++)
{ a[j]=rand()%10;
cout<<a[j];}
}}

First you need to learn how to properly format your program -- that is one of the sloppiest format methods I've seen in a long time. Here is an example Notice that you should use spaces very plentifully to make your program easier to read. Curly braces go on a line all by themselves to make them easy to find and match up.

And ust int main() -- never ever void main().

int main()
{ 
    for(int i=0; i <= 100; i++)
    {
        int a[10];
        for (int j=0;j<=10;j++)
       { 
           a[j] = rand() % 10;
          cout << a[j];
       }
    }
    return 0;
}

Now for your question: after generating a random number you need to search the array to see if it already exists. If it does, then generate another random number. You will probably want to put that into another function because you have to use it for several arrays.

int generate(int maxvalue, int array[], int arraySize)
{
   // put your code here which loop that generates
   // a random number and search the array 
   // for existance.  

}

u can help with that problem im facing..instead of giving silly instructions..;) thanx

yeah..its urgent!!!! lolz!

u could

std::set<int> unique_ints ;
// N == number of elements
// MAX_VALUE == max value of an element
while( unique_ints.size() < N )
   unique_ints.insert( std::rand()%MAX_VALUE ) ;
//iterate from unique_ints.begin() to 
// unique_ints.end() and populate the array

for your specific problem:
1-100 arrays of size 10 with randomly distinct numbers from (0-9)
2-100 arrays of size 50 with randomly distinct numbers from (0-49)
3-100 arrays of size 100 with randomly distinct numbers from (0-99)
4-100 arrays of size 200 with randomly distinct numbers from (0-199)

u could fill an array of size N with values
0, 1, 2, ..., N-2, N-1
and then just do a std::random_shuffle on it.
(after seeding the random number generator)

commented: Excellent suggestion, but probably too advanced to hand in as noob homework +6

There are other problems..
"Generate 100x4 unique arrays" even rand() won't exactly be rand() if you have a fast enough machine..

Here is one way:

void fill_array( int* arr, const int len, const int max )
{
    vector<int> vi ; //or use set if you like..
    srand(time(0)) ;
    int tmp ;
    while( vi.size() < len )
    {
        tmp = rand() % (max+1) ;
        if( vi.end() == find(vi.begin(), vi.end(), tmp ) )
            vi.push_back(tmp) ;
    }
    memcpy( (void*)arr, (const void*)(&vi[0]), sizeof(int)*len) ;
}

void print_arr( int* arr, int len )
{
    for( int i = 0; i < len; i++ )
        cout << arr[i] << ' ' ;
}

int main()
{
    int a10[10], a50[50], a100[100], a200[200] ;

    for( int i = 0; i < 100; i++ )
    {
        Sleep(1000) ; //without this all 100 times a10 will be filled
                             //with same sequence as we do srand(time(0));

        fill_array( a10, 10, 9 ) ;
        cout << endl << "-----a10-----" << endl ; print_arr( a10, 10 ) ;

        fill_array( a50, 50, 49 ) ;
        cout << endl << "-----a50-----" << endl ; print_arr( a50, 50 ) ;

        fill_array( a100, 100, 99 ) ;
        cout << endl << "-----a100-----" << endl ; print_arr( a100, 100 ) ;

        fill_array( a200, 200, 199 ) ;
        cout << endl << "-----a200-----" << endl ; print_arr( a200, 200 ) ;
    }
    return 0;
}

Then again there are other solutions:
Given the problem stmt:
1-100 arrays of size 10 with randomly distinct numbers from (0-9)
=>100 arrays of size X with randomly distinct numbers from (0-(X-1))
We can also do this:
Fill up the array of size X with values from 0 to X-1 so that element at index 0 has value 0 and so on..
Pass the array to a function that goes over the array once and swaps current element with randomly selected element.

void fill_array( int* arr, const int len ) //max is len - 1 implicitely
{
    static long seed = time(0) ;
    srand( (seed += time(0)) ) ;
    for( int i = 0; i < len; i++ )
        arr[i] = i ;

    for( i = 0; i < len; i++ )
    {
        int rand_index = rand() % len ;
        int tmp = arr[rand_index] ;
        arr[rand_index] = arr[i] ;
        arr[i] = tmp ;
    }
}

This would need less (and finite) # of calls to s/rand() than other method. Though this is meant for testing so no marks for that.. :confused:

u can help with that problem im facing..instead of giving silly instructions..;) thanx

You could help yourself by reading the link in Salem's post. It's not our problem that this is urgent, and saying so won't make us reply any faster. If you want the fastest replies possible, I suggest reading the entire article.

There are other problems..
"Generate 100x4 unique arrays" even rand() won't exactly be rand() if you have a fast enough machine..

Here is one way

Simply horrid. There are better ways of generating random numbers than making the machine sleep.
http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx

There are other problems..
"Generate 100x4 unique arrays" even rand() won't exactly be rand() if you have a fast enough machine..

What's this supposed to mean? Random has nothing to do with the speed of your machine...

What's this supposed to mean? Random has nothing to do with the speed of your machine...

It does if you use time(0) as seed for srand().
The problem is that you need to come up with 100 different random series with a single call to your program. (not running it multiple times).
Issue is in coming up with 100 different seeds.
This is exactly what I've 'solved' using "Sleep(1) + seed(time(0)" in first example and "static long seed = time(0) ; + srand( (seed += time(0)) ) ;" in second. (and what JP called "simply horrid")

Simply horrid. There are better ways of generating random numbers than making the machine sleep.
http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx

Indeed it is. :)
But then 1. It solves the problem 2. There is no performance requirement. and 3. I have given another way in second example.
Anyway, it's interesting that you posted this link because while I was just fiddling with the code I posted I had checked a few links to find a simple way to solve the problem.
Now I checked the link you posted and there not a single way that can actually generate 100 different random sequences ! Is there another way? (apart from the one I gave in my second example)

it isn't necessary to call srand() more than once during the lifetime of a program. The only way to generate an array of unique random numbers is to generate a random number then search the array to see if it has already been generated. If it has, then generate another random number and search again (as in your example program). calling srand() frequently will not help with this.

>Now I checked the link you posted and there not a single way that can actually generate 100 different random
>sequences !
What do you mean by that? Simply seed the random number generator as shown in the article, and then go ahead and generate your random numbers. You'll still have to search the array to check that the number isn't already taken, but that's expected.

>Is there another way? (apart from the one I gave in my second example)
I'm with Salem on this one. Filling up the array with sequential numbers, and then using std::random_shuffle to mix them up like vijayan121 suggested is probably the easiest way.

>>Now I checked the link you posted and there not a single way that can actually generate 100 different random sequences !
What do you mean by that? Simply seed the random number generator as shown in the article, and then go ahead and generate your random numbers. You'll still have to search the array to check that the number isn't already taken, but that's expected.

What was meant is there is no code that can generate 100x4 different seeds (which is what I did using Sleep(1)).
Anyway, it's actually not needed to generate 400 unique seeds like AD mentioned above just seed it once and keep using rand().

>>Is there another way? (apart from the one I gave in my second example)
I'm with Salem on this one. Filling up the array with sequential numbers, and then using std::random_shuffle to mix them up like vijayan121 suggested is probably the easiest way.

That's what my second example also does. :) std::random_shuffle is surely the simpler/easier way though..

Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin - John Von Neumann (1951).

for more information see:
http://www.random.org/randomness/

if pseudo-randomness is all that is required, a good c++ library is
the boost random number library.
for more information on these, see
http://www.boost.org/libs/random/random-generators.html
http://www.boost.org/libs/random/random-distributions.html
http://www.boost.org/libs/random/random-concepts.html

thanx for ur replys but i solved it..eventually..in a very easier way...
thanx..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.