Thread: How can I asign a random values to an array without repeating the value. Please help.

  1. #16
    Registered User MarkSquall's Avatar
    Join Date
    Aug 2007
    Posts
    27

    Talking Thank you everyone! It's a big help.

    Dear everyone,

    For those who replied on my thread, thank you so much. I was not able to visit this site for a while that's why I am surprised to have many replies from you all, actually my last reading is on Mr. King Mir reply, that's why I clicked his link Shuffleing algorithms can be found on Google, and I visited one of the site and I tried to apply its code, here it is (with some little modifications):
    Code:
    #include<stdio.h>
    int main()
    {
         int i, j, k;
         for (i = 0; i < 9; i++)
              num[i] = i; /*assign 0 to num[0], assign 1 to num[1], and so on...*/
         
        for (i = 0; i < 9; i++) {
              j = rand() % n;
              k = num[i];
              num[i] = num[j];
              num[j] = k;	
        }
    }
    And it works, although it seems that the same arrangement of randoming numbers we're encountered, but at least there were no more repeating value encountered, that is, no more two 4's, three 5's, etc. (may be it's because I initialized them before I shuffled it). As I have read again this thread, I guess this what's laserlight is telling beforehand, to "populate" first the array, then do the shuffling.

    But nevertheless, I am already contented with the result. I'll try everyones codes as another workarounds in this related problem. Any way, any more tutorial on what this rand() function? I know it's a randoming function, but I want to know about it more.

    Thank you and more power to everyone.


    Respectfully Yours,

    MarkSquall

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I know it's a randoming function, but I want to know about it more.
    This covers random number generators in the general sense and this covers rand specifically.
    My best code is written with the delete key.

  3. #18
    Registered User MarkSquall's Avatar
    Join Date
    Aug 2007
    Posts
    27

    Smile Thank you Sirs/Madams.

    Thanks again everyone for the information. What makes really fun here is solving the problem I have in C/C++ programming together with everyone's help. I am really glad I come across to this site.


    I have more questions to post here soon, so I hope everyone will be patient enough to teach me C and C++. God bless.


    Respectfully Yours,

    MarkSquall

    P.S.
    But how about Java? Is there a nice forum like this one about Java programming? Hope someone could share it too.
    "Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-19-2008, 02:36 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Eliminating duplicate values from a 20x2 array
    By desipunjabi in forum C Programming
    Replies: 2
    Last Post: 10-29-2005, 09:11 PM
  4. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM