Thread: Problem creating unique random numbers

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    106

    Problem creating unique random numbers

    I'm not sure whats wrong...

    Code:
    #include <iostream>
    #include "math.h"
    #include "time.h"
    
    
    
    char *shapes[2] = {"ball", "cube"}; // two arrays for color and shape
    char *colors[4] = {"red", "blue", "orange", "green"};
    
    int selectObject(int, int); //function prototype
    
    int main(){
        int color = 0; //numbers to be used for selection and display
        int shape = 0;
        
        
        
        int loopNumber;
        for (loopNumber = 0; loopNumber <= 100; loopNumber++){
            selectObject(color, shape);
            std::cout<< colors[color]<< " "<< shapes[shape];
        }
        
        std::cin.get();
    }
    
    int selectObject(int color, int shape){
        std::srand(time(NULL)); // I thought this was to make each random number different?
        
        color = std::rand() % 3; // gets random number to decide member of array to display
        shape = std::rand() % 1;
        
        return color;
        return shape;
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You might find this article interesting: Eternally Confuzzled - Using rand()

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    1. Two return statements unconditionally after each other are useless.
    2. Modulo 1 is useless.
    3. I think modulo 3 isn't what you intended either...

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Code:
    #include <iostream>
    #include <math.h>
    #include <time.h>
    
    const char* shapes[2] = {"ball", "cube"}; // two arrays for color and shape
    const char* colors[4] = {"red", "blue", "orange", "green"};
    
    void selectObject(int& color, int& shape)
    {
        color = std::rand() % 4; 
        shape = std::rand() % 2;
    }
    
    int main()
    {
        std::srand(time(NULL)); 
    
        int color = 0;
        int shape = 0;
       
        for ( int loopNumber = 0; loopNumber <= 100; loopNumber++)
       {
            selectObject(color, shape);
            std::cout<< colors[color]<< " "<< shapes[shape];
        }
        
        std::cin.get();
    }
    This is the code you probably meant to write. Note that the function is now able to change it's parameters back in the main method because they are passed by reference and doesn't return anything. srand is called just once to seed the random number generator. Note that it's still random. 1,1,1,2,1,3,1,4,1,1,5,3 is a perfectly random sequence. Compare ordinary dice. They don't automatically roll a six once you rolled a 1,2,3,4,5.

    You need to use the array size for your modulo operation (read the article posted why this might be bad if you need perfect random numbers... I guess it's fine for now). I replaced 3 and 1 by 4 and 2 but you might want to keep that numbers somewhere else in a central place as constants or #defines.

    What did you expect to get anyway? There are 8 possible combinations and you get 100 randoms, there is no way to get 100 unique values out of 8 possible variations. If you need to get 8 combinations in random order, I'd suggest to generate all 8 and shuffle them.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    106
    Thanks nvoigt.. I now understand... And the only reason it was set to 100 was to make sure it wasn't just sptting the same random numbers out then they change

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Creating Random hex numbers
    By disruptor108 in forum C++ Programming
    Replies: 24
    Last Post: 06-06-2007, 11:00 AM
  3. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. Unique Random Numbers in 2D Array
    By kssjbr in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2003, 02:45 AM