|
-
January 1st, 2012, 12:06 PM
#1
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?
-
January 1st, 2012, 12:27 PM
#2
Re: Srand function problem and a different problem
 Originally Posted by revve
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?
 Originally Posted by revve
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() % (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
-
January 1st, 2012, 01:16 PM
#3
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.
-
January 1st, 2012, 02:15 PM
#4
Re: Srand function problem and a different problem
 Originally Posted by revve
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
-
January 1st, 2012, 02:18 PM
#5
Re: Srand function problem and a different problem
 Originally Posted by revve
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:
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
-
January 1st, 2012, 03:36 PM
#6
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.
-
January 1st, 2012, 05:34 PM
#7
Re: Srand function problem and a different problem
 Originally Posted by revve
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
-
January 1st, 2012, 05:40 PM
#8
Re: Srand function problem and a different problem
 Originally Posted by revve
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
-
January 2nd, 2012, 01:35 PM
#9
Re: Srand function problem and a different problem
 Originally Posted by revve
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
-
January 4th, 2012, 09:30 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|