Question:

How would I be able to do this array?

by  |  earlier

0 LIKES UnLike

I posted a short part of my code down here.

What I'm intending to do is to try to get the function

to produce 52 non recurring numbers.. From what I

heard, I have to create an array, and check against it

However, i am not really good with arrays, so I would

require some help at this part. From what I have

already in the code, what can be modified to get something

more like what I want?

I need to know how to do the "checking the array for already

existent numbers" part. How can it be done? :)

int random_integer;

int array[52];

int main()

{

srand((unsigned)time(0));

for(int index=0; index<52; index++)

{array[index] = (rand()%51)+1;

cout << array[index] << endl;}

system ("pause");

}

Thank you so much people!

(P.S. Give me some code to work with ^^)

 Tags:

   Report

4 ANSWERS


  1. it is probably easier to generate the list of numbers you need and then to randomise that list. if you pick a random number each time, and then check that number against your list it becomes very inefficient.

    so:

    - create an array containing 0-51

    - create another array and fill it by selecting a number at random from those remaining in the array (i.e. you have to remove a number each time it is selected somehow)

    i have included a link which has something similar


  2. I&#039;ve not done programming for a while but here goes:

    http://pastebin.com/f232064a2

    I don&#039;t know whether this will compile and you didn&#039;t specify java or c++ for example (I guessed c++). Try and play around with it it may put you on the right track. Incidently you didn&#039;t give your array a name so i called it myarray instead.


  3. i made something similar to that. first create an array, then, in a loop assign a random value to ith value of the array. in another loop inside the main loop check if that value is assigned already to another member of array. something like this:

    int array[52];

    int i=0,j=0,h;

    while(i&lt;52)

    k:h=rand();

    for(j=0;j&lt;i;j++)

    {

    if(array[j]==h)

    {

    goto k;

    }

    }

    array[i]=h;

    i++;

    }


  4. Preload the array with the numbers 1 to 52.  When you pick an element at random, if it&#039;s nonzero, cout its value and then set its value to zero, otherwise try again.  Then think of a couple tricks to speed it up.

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.