CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2012
    Posts
    4

    Srand function problem and a different problem

    #include <iostream>
    using std::cout;
    using std::endl;

    #include <cstdlib>
    #include <ctime>

    #include <iomanip> //In this line, compiler gives warning C4067: unexpected tokens following //preprocessor directive - expected a newline but program still executes instead of this. //What is the reason of this error? How can I fix it?
    using std::setw;

    using namespace std;



    Another question,

    I have the following code in my constructor:

    srand(int(time(NULL)));
    for(int i=0; i<(99-1); i++){ //shuffling card;

    int r = i + (rand() % (99-i)); // Random remaining position.
    int temp = kartSayilar;
    kartSayilar = kartSayilar[r];
    kartSayilar[r] = temp;
    }

    The aim of this is to shuffle numbers in the kartSayilar array randomly. I creates two objects of this in a different .cpp file but when program executes, both objects have the same random numbers. I think my srand function doesn't work. How can i fix it?

  2. #2
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Srand function problem and a different problem

    Quote Originally Posted by revve View Post
    Code:
    #include <iostream> 
    using std::cout; 
    using std::endl; 
     
    #include <cstdlib> 
    #include <ctime> 
     
    #include <iomanip> //In this line, compiler gives warning C4067: unexpected tokens following //preprocessor directive - expected a newline but program still executes instead of this. //What is the reason of this error? How can I fix it?
    using std::setw; 
     
    using namespace std;
    Have you tried moving the using directives to after the includes?

    Quote Originally Posted by revve View Post
    Another question,

    I have the following code in my constructor:

    Code:
    srand(int(time(NULL))); 
    for(int i=0; i<(99-1); i++){ //shuffling card; 
     
    int r = i + (rand() &#37; (99-i)); // Random remaining position. 
    int temp = kartSayilar; 
    kartSayilar = kartSayilar[r]; 
    kartSayilar[r] = temp; 
    }
    The aim of this is to shuffle numbers in the kartSayilar array randomly. I creates two objects of this in a different .cpp file but when program executes, both objects have the same random numbers. I think my srand function doesn't work. How can i fix it?
    Where is the index to kartSayilar on the third line in the for loop? I think you want:
    Code:
    srand(int(time(NULL))); 
    for(int i=0; i<(99-1); i++){ //shuffling card; 
     
        int r = i + (rand() % (99-i)); // Random remaining position. 
        int temp = kartSayilar[i]; 
        kartSayilar[i] = kartSayilar[r]; 
        kartSayilar[r] = temp; 
    }
    -Erik

  3. #3
    Join Date
    Jan 2012
    Posts
    4

    Re: Srand function problem and a different problem

    a::a(){

    for(int i=0; i<99; i++){
    kartSayilar[i] = i+1;
    }
    srand(int(time(NULL)));
    for(int i=0; i<(99-1); i++){ //shuffling card;

    int r = i + (rand() % (99-i)); // Random remaining position.
    int temp = kartSayilar[i];
    kartSayilar[i] = kartSayilar[r];
    kartSayilar[r] = temp;
    }
    }

    my constructor is like that.

    a array[2];

    array [0] = a();
    array [1] = a();

    array[0].display() and array[1].display() gives the same randomly shuffled kartSayilar. I want them differently.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Srand function problem and a different problem

    Quote Originally Posted by revve View Post
    The aim of this is to shuffle numbers in the kartSayilar array randomly.
    Just fill the array with random numbers from 1 to 100. Then shuffle the array using random_shuffle.

    Also, you should be seeding the random number generator only once. There is no need to keep seeding it over and over again every time you create an "a" instance.

    Here is an example:
    Code:
    #include <algorithm>
    #include <cstdlib>
    #include <ctime>
    
    class SomeClass
    {
    	int kartSayilar[100];
    
            // helper class to seed only once. 
    	struct Seeder
    	{ Seeder() { srand(int(time(NULL))); } };
    
            static int Randomizer()
            {
    	    return rand()% 99;
            }
    
    public:
    	SomeClass();
    
    };
    
    
    SomeClass::SomeClass()
    {
    	static Seeder randSeed;  // only called once due to it being static
    
            // get the number of items in array
    	const int numItems = sizeof(kartSayilar) / sizeof(kartSayilar[0]);
    
            // generate random numbers
    	std::generate(kartSayilar, kartSayilar + numItems, Randomizer);    
    
            // shuffle them
    	std::random_shuffle( kartSayilar, kartSayilar + numItems );
    }
    
    int main()
    {
    	SomeClass s1[2];
    }
    The main point is that the Seeder instance in the constructor is static. This means that it will be called only once, regardless of the number of times the object is created.

    Also, usage of generate() and random_shuffle() were done to generate and shuffle the numbers. The Randomizer is a helper function that returns the random number to fill into the array.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Srand function problem and a different problem

    Quote Originally Posted by revve View Post
    my constructor is like that.

    a array[2];

    array [0] = a();
    array [1] = a();

    array[0].display() and array[1].display() gives the same randomly shuffled kartSayilar. I want them differently.
    This one line:
    Code:
    a array[2];
    already creates two a objects. There is no need for these lines:
    Code:
    array [0] = a();
    array [1] = a();
    Look at my example. The SomeClass is an array of 2 items, and if you inspect it, you will see that the numbers in the arrays are different.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jan 2012
    Posts
    4

    Re: Srand function problem and a different problem

    well, thank you about it.

    But actually, I'm trying to write game called Tombala. I want user to enter the number of the players. Each kartSayilar array is a part of the player's card. Thats why I create this instances in the main. I'm going to send PM to you. I hope you'll understand and help me.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Srand function problem and a different problem

    Quote Originally Posted by revve View Post
    ach kartSayilar array is a part of the player's card. Thats why I create this instances in the main.
    What is the difference between what I posted and what you originally posted? The difference is my version works, and yours doesn't. My version does exactly as you described -- two instances are created in main() of the "a" (I called it SomeClass) class.

    The issue you really have is not a design issue, it's an issue with understanding how C++ works when it comes to creating objects.

    If you have an array of objects, just declaring the array will call the default constructor for all the elements. You do not need these lines of code:
    Code:
    /*array [0] = a();
    array [1] = a();*/  These are not needed
    If you don't believe me, look at what array[0] and array[1] contain after declaring the array.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Srand function problem and a different problem

    Quote Originally Posted by revve View Post
    I'm going to send PM to you. I hope you'll understand and help me.
    First, use code tags when posting code. Look at how my posted code looks, and then compare it with your post. Your code is almost unreadable without it.

    I suggest posting your code here, and ask questions.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Srand function problem and a different problem

    Quote Originally Posted by revve View Post
    a::a(){

    for(int i=0; i<99; i++){
    kartSayilar[i] = i+1;
    }
    srand(int(time(NULL)));
    for(int i=0; i<(99-1); i++){ //shuffling card;

    int r = i + (rand() % (99-i)); // Random remaining position.
    int temp = kartSayilar[i];
    kartSayilar[i] = kartSayilar[r];
    kartSayilar[r] = temp;
    }
    }

    my constructor is like that.

    a array[2];

    array [0] = a();
    array [1] = a();

    array[0].display() and array[1].display() gives the same randomly shuffled kartSayilar. I want them differently.
    Note 1: You are already filling the arrays in the declaration. You don't need to implicitly call the function.

    Note 2: Because you are calling srand() both times with time(), which has a one second resolution, you are calling srand() with the same value which will generate the same pseudo-random array from rand(). Try calling srand() once, before declaring the arrays.

    -Erik

  10. #10
    Join Date
    Jan 2012
    Posts
    4

    Re: Srand function problem and a different problem

    thank you guys. I resolved the problem. I called srand() in main() then i created objects successfully

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured