Random Generation

This is a discussion on Random Generation within the C++ Programming forums, part of the General Programming Boards category; Hi guys! I'm writing a program that involves with random generation. Basically, I want to randomly generate a range of ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    22

    Random Generation

    Hi guys!

    I'm writing a program that involves with random generation. Basically, I want to randomly generate a range of values, lets say from 1 to 10 to an array of 10 element. Another thing is I also want those generated values to not overlap each other like (1,2,2,3,3,4,5...) something like that. I want to guarantee that each value will only happen once in the array from a certain range.
    I hope that u guys can give me a hand and help me with this, really appreciate that ^_^

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Cool.

    What have you done so far?
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    22
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #define MAX 20
    
    void randomvalues(int *l, int n) {                     
        srand(time(0));                                     
        for(int i=0; i<n;i++) {                   
        l[i] = (rand()%n);                        
        }
    }  
    
    int main() {  
        void randomvalues(int *l, int n);        
        
        int a[MAX];                             
    
        randomvalues(a, MAX);
    This is what i got so far. I still couldn't find a way to make it none overlapping.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    The solution that you want is going to somehow break down to using an array (though, if the numbers only go from 1 -> 10, you could use an int.)

    You'll want to save off that a number had been used and generate a new number until you find one that isn't used. That, or count up/down from your used position until you find some number that isn't used, the choice is up to you.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    Denmark
    Posts
    52
    Start with an array of appropriate size and populated with the numbers 1,2,3...n and then randomize it by swapping, numerous times, random element a with random element b. You will thus avoid duplicates appearing. This will also allow you to insert duplicates at some later point if necessary.
    Last edited by Fossaw; 04-09-2010 at 01:03 AM.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    You just need an equation that gives different results every time. You should store a value that
    keeps incrementing ( or else ), and you'll pass it to the eq to get a 'random' number.

    Code:
    int equation()
    {
         return 2*x-1; 
    } 
    
    int x = 0; // Or any other value 
    
    int rValue = equation(x++);
    Of course, that's one way of doing this...
    Devoted my life to programming...

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438

    .

    maybe something like this..

    Code:
        int num_array[10] = //initialise  1 to 10
        int count = 0;
        int results_array[10] = { 0 };
    
        int num = 0;
        
        while(count < 10)
        {
            num = //choose rand out of 10
            
            if(num_array[num] != 0) //if not already used
            {
                results_array[count] = num_array[num]; //fill array
                num_array[num] = 0; // set to used
                count++;
            }
        }
    if thats the sort of thing you mean, i dunno, this is similar to what fossaw said

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    I second Fossaw's idea, though not the exact suggestion as the shuffling method described is not particularly good if you want a uniform distribution, and besides... use std::random_shuffle().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2010
    Location
    Denmark
    Posts
    52
    It has the advantage of being easy to understand and code for a relative newcomer.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    22
    thanks for the suggestion guys!
    just a curious question, random_shuffle() does what? is it efficient to random a array?

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by davewang View Post
    thanks for the suggestion guys!
    just a curious question, random_shuffle() does what? is it efficient to random a array?
    Look here and especially the example.
    Generally, it radnomizes the elements of a structure, like a std::vector.
    It is efficient in general and it only lets you use random access iterators (thus you wouldn't be able to use a std::list, but a std::vector). Exactly how efficient I don't know, you will need to look at the implementation of the function.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Quote Originally Posted by davewang
    random_shuffle() does what?
    It shuffles the elements in the specified range with uniform distribution. This range can be the entire range of an array, but as C_ntua noted, it must have random access to the range, and certainly pointers to the elements of an array qualify.

    Quote Originally Posted by davewang
    is it efficient to random a array?
    It is as efficient as what Fossaw described, if you interpret "numerous times" to be "one time less than the number of elements in the array". But it is better if you want a uniform distribution, and of course better in the sense that you do not have to code it yourself so you reduce the chance of introducing a bug.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Diablo's random level generation
    By glo in forum Game Programming
    Replies: 7
    Last Post: 07-19-2008, 03:04 AM
  3. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 04:23 PM
  4. biased random numb generation
    By estranged in forum C Programming
    Replies: 4
    Last Post: 12-13-2003, 05:00 AM
  5. fast random number generation
    By Heraclitus in forum C Programming
    Replies: 4
    Last Post: 02-09-2003, 06:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21