C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why isn't srand working?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
David Shaw
Guest





PostPosted: Wed Mar 03, 2004 7:33 pm    Post subject: Why isn't srand working? Reply with quote



Hi,
I'm writing a fun little program to play "Petals Around the Roses"
with... I need to seed my random numbers so that they won't be the
same every time I run the program, but my compiler won't let me.
Here's the output my compiler generated...:
--
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:Documents and SettingsrmarkoffDesktoproses.cpp" -o
"C:Documents and SettingsrmarkoffDesktoproses.exe"
-I"C:Dev-Cppincludec++" -I"C:Dev-Cppincludec++mingw32"
-I"C:Dev-Cppincludec++backward" -I"C:Dev-Cppinclude"
-L"C:Dev-Cpplib"
C:/Documents and Settings/rmarkoff/Desktop/roses.cpp:7: ISO C++
forbids
declaration of `srand' with no type

C:/Documents and Settings/rmarkoff/Desktop/roses.cpp:7: `int srand'
redeclared
as different kind of symbol

C:/Dev-Cpp/include/stdlib.h:362: previous declaration of `void
srand(unsigned
int)'

Execution terminated
--

And here's my code:

#include iostream> // I took out the <
#include stdlib.h> // in these #includes
#include time.h> // so that they will work on the web.

using namespace std;

srand((unsigned)time(NULL));


int matrix[4];

void display(void) { // Start Display()

int x;

for(x=0;x<4;x++) { // Randomize matrix

matrix[x] = (rand() % 6) + 1;

} // End for()


std::cout << "n----------------------------------" << std::endl;
std::cout << "|" << matrix[0] << "|" << "|" << matrix[1] << "|" <<
"|"
<< matrix[2] << "|" << matrix[3] << "|" <<
matrix[4] << std::endl;
std::cout << "----------------------------------" << std::endl;

} // end display()

int main() { // Start main()

display();
std::system("pause");

} // End main


.... thanks for any help you can give me. :)


- David Shaw
Back to top
Lew Pitcher
Guest





PostPosted: Wed Mar 03, 2004 7:43 pm    Post subject: Re: Why isn't srand working? Reply with quote



David Shaw wrote:

Quote:
Hi,
I'm writing a fun little program to play "Petals Around the Roses"

Aha! I wrote a k&r C version of this in the mid 80s. Kewl puzzle, ain't it?


Quote:
with... I need to seed my random numbers so that they won't be the
same every time I run the program, but my compiler won't let me.
Here's the output my compiler generated...:
--
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:Documents and SettingsrmarkoffDesktoproses.cpp" -o
"C:Documents and SettingsrmarkoffDesktoproses.exe"
-I"C:Dev-Cppincludec++" -I"C:Dev-Cppincludec++mingw32"
-I"C:Dev-Cppincludec++backward" -I"C:Dev-Cppinclude"
-L"C:Dev-Cpplib"
C:/Documents and Settings/rmarkoff/Desktop/roses.cpp:7: ISO C++
forbids

OK, you're off topic for comp.lang.c

You are still on topic for comp.lang.c++, though

[snip]

Followups set to comp.lang.c++


--
Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Back to top
Christopher Benson-Manica
Guest





PostPosted: Wed Mar 03, 2004 7:46 pm    Post subject: Re: Why isn't srand working? Reply with quote



In comp.lang.c David Shaw <spamthisemail (AT) mailinator (DOT) com> wrote:
^^^^^^^^^^^

Please don't post C++ code to comp.lang.c in the future.

Quote:
C:/Documents and Settings/rmarkoff/Desktop/roses.cpp:7: ISO C++
forbids
declaration of `srand' with no type

C:/Documents and Settings/rmarkoff/Desktop/roses.cpp:7: `int srand'
redeclared
as different kind of symbol

using namespace std;

srand((unsigned)time(NULL));

Your compiler is trying to tell you something, namely that you can't
call functions outside of functions, as you are trying to do. It
thinks you're declaring a prototype, and is consequently becoming
confused.

Quote:
#include iostream> // I took out the
#include stdlib.h> // in these #includes
#include time.h> // so that they will work on the web.

Any newsreader that can't handle something simple like <iostream>
deserves to be shot anyway - don't bother with it in the future.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Back to top
Andrey Tarasevich
Guest





PostPosted: Wed Mar 03, 2004 7:48 pm    Post subject: Re: Why isn't srand working? Reply with quote

David Shaw wrote:
Quote:
...
#include iostream> // I took out the
#include stdlib.h> // in these #includes
#include time.h> // so that they will work on the web.

using namespace std;

srand((unsigned)time(NULL));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

What is this doing here and what did you mean by this?

--
Best regards,
Andrey Tarasevich


Back to top
Greg Schmidt
Guest





PostPosted: Wed Mar 03, 2004 7:48 pm    Post subject: Re: Why isn't srand working? Reply with quote

On 3 Mar 2004 11:33:22 -0800, David Shaw wrote:

Quote:
Hi,
I'm writing a fun little program to play "Petals Around the Roses"
with... I need to seed my random numbers so that they won't be the
same every time I run the program, but my compiler won't let me.

You are trying to call srand outside of any function, which gcc is taking
to be an attempt to declare the function prototype. Move the call inside
of your main function, and all should be well.

--
Greg Schmidt [email]gregs (AT) trawna (DOT) com[/email]
Trawna Publications http://www.trawna.com/

Back to top
Leor Zolman
Guest





PostPosted: Wed Mar 03, 2004 7:51 pm    Post subject: Re: Why isn't srand working? Reply with quote

On 3 Mar 2004 11:33:22 -0800, [email]spamthisemail (AT) mailinator (DOT) com[/email] (David Shaw)
wrote:

Quote:
Hi,
I'm writing a fun little program to play "Petals Around the Roses"
with... I need to seed my random numbers so that they won't be the
same every time I run the program, but my compiler won't let me.
Here's the output my compiler generated...:
--
[snip]


Best thing is just to copy and paste the code as-is right into your
messages. Don't worry about "<" characters, they won't bite ;-)

If you simply move that "srand" call into your main() function from where
it is (out at file scope), your program works fine.
-leor


Leor Zolman
BD Software
[email]leor (AT) bdsoft (DOT) com[/email]
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html

Back to top
Mike Wahler
Guest





PostPosted: Wed Mar 03, 2004 7:52 pm    Post subject: Re: Why isn't srand working? Reply with quote


"David Shaw" <spamthisemail (AT) mailinator (DOT) com> wrote

Quote:
Hi,
I'm writing a fun little program to play "Petals Around the Roses"
with... I need to seed my random numbers so that they won't be the
same every time I run the program, but my compiler won't let me.
Here's the output my compiler generated...:
--
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:Documents and SettingsrmarkoffDesktoproses.cpp" -o
"C:Documents and SettingsrmarkoffDesktoproses.exe"
-I"C:Dev-Cppincludec++" -I"C:Dev-Cppincludec++mingw32"
-I"C:Dev-Cppincludec++backward" -I"C:Dev-Cppinclude"
-L"C:Dev-Cpplib"
C:/Documents and Settings/rmarkoff/Desktop/roses.cpp:7: ISO C++
forbids
declaration of `srand' with no type

C:/Documents and Settings/rmarkoff/Desktop/roses.cpp:7: `int srand'
redeclared
as different kind of symbol

C:/Dev-Cpp/include/stdlib.h:362: previous declaration of `void
srand(unsigned
int)'

Execution terminated
--

And here's my code:

#include iostream> // I took out the

Don't.

Quote:
#include stdlib.h> // in these #includes
#include time.h> // so that they will work on the web.

It's not 'the web', but some news readers 'mangle' such
characters. That's their problem, not yours. Post
compilable code if at all possible.

Quote:

using namespace std;

srand((unsigned)time(NULL));

This statement doesn't do what you probably think it does.
It does *not* invoke the 'srand()' function but (incorrectly)
declares it. (Executable statements are not allowed outside
of a function). This declaration conflicts with the correct
one provided by <stdlib.h>. Remove this line. Call 'srand()'
at the beginning of 'main()'.

Quote:


int matrix[4];

void display(void) { // Start Display()

int x;

for(x=0;x<4;x++) { // Randomize matrix

matrix[x] = (rand() % 6) + 1;

See the C FAQ for a better way to generate random numbers.

Quote:

} // End for()


std::cout << "n----------------------------------" << std::endl;
std::cout << "|" << matrix[0] << "|" << "|" << matrix[1] << "|"
"|"
matrix[2] << "|" << matrix[3] << "|"
matrix[4] << std::endl;
std::cout << "----------------------------------" << std::endl;

} // end display()

int main() { // Start main()


srand((unsigned int)time(0));

Quote:
display();
std::system("pause");

Note that while 'system()' is standard, its
argument is not.

Quote:

} // End main


... thanks for any help you can give me. :)


- David Shaw

Finally, note that your code is topical for comp.lang.c++,
but not for comp.lang.c. I've removed the latter from
the 'newsgroups' list.

-Mike





Back to top
Ben Pfaff
Guest





PostPosted: Wed Mar 03, 2004 7:55 pm    Post subject: Re: Why isn't srand working? Reply with quote

[email]spamthisemail (AT) mailinator (DOT) com[/email] (David Shaw) writes:

Quote:
#include iostream> // I took out the
#include stdlib.h> // in these #includes
#include time.h> // so that they will work on the web.

Don't post C++ to comp.lang.c.
--
Go not to Usenet for counsel, for they will say both no and yes.

Back to top
Malcolm
Guest





PostPosted: Wed Mar 03, 2004 8:00 pm    Post subject: Re: Why isn't srand working? Reply with quote


"David Shaw" <spamthisemail (AT) mailinator (DOT) com> wrote in message
Quote:
using namespace std;

srand((unsigned)time(NULL));

Either move this to main() or, if you want to play with C++, declare an

object

class Randomiser
{
public:
Randomiser(void) { srand( (unsigned) time(NULL)); };
};

Randomiser myengine;

This will cause the call to srand to be executed some time at program
startup. It's useful if you want to provide a library that relies on seeding
rand(), but doesn't have access to main().



Back to top
Keith Thompson
Guest





PostPosted: Wed Mar 03, 2004 9:51 pm    Post subject: Re: Why isn't srand working? Reply with quote

[email]spamthisemail (AT) mailinator (DOT) com[/email] (David Shaw) writes:
[...]
Quote:
And here's my code:

#include iostream> // I took out the
#include stdlib.h> // in these #includes
#include time.h> // so that they will work on the web.

You're not on the web. This is Usenet, which has no problem with '<'
and '>' characters. Usenet is a plain text medium; HTML is largely
irrelevant.

And, as others have mentioned, your code is C++, so it's off-topic in
comp.lang.c.

--
Keith Thompson (The_Other_Keith) [email]kst-u (AT) mib (DOT) org[/email] <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"

Back to top
David Shaw
Guest





PostPosted: Thu Mar 04, 2004 6:43 am    Post subject: Re: Why isn't srand working? Reply with quote

<snippy>
Many apologies, I really haven't used usenet all that much... when I
had, it was usually about functions common to C and C++... sorry to
the comp.lang.c readers who must have gotten a little miffed at me :

Okay, that makes sense... I'll stick that back into a function, rather
than having it in the very beginning of the program. On another note,
what exactly does srand() *do*? All I know is that using rand() after
it produces seeded random numbers, which are different each time the
program's run, and it has something to do with the system clock... can
anyone help me with this one? It's quite frustrating to not be able to
fix things since you've no idea what you're supposed to be doing.

Oh, and about the previous < withdrawl... I was using GoogleGroups,
and forgot that... meh, it doesn't matter. Thanks for the help
everyone, and sorry *.c users... heh.


- David
http://david.artoo.net/
Back to top
Mike Wahler
Guest





PostPosted: Thu Mar 04, 2004 9:19 am    Post subject: Re: Why isn't srand working? Reply with quote

"David Shaw" <spamthisemail (AT) mailinator (DOT) com> wrote

Quote:
snippy
Many apologies, I really haven't used usenet all that much... when I
had, it was usually about functions common to C and C++... sorry to
the comp.lang.c readers who must have gotten a little miffed at me :

Okay, that makes sense... I'll stick that back into a function, rather
than having it in the very beginning of the program. On another note,
what exactly does srand() *do*?

It seeds the random number generator.

Quote:
All I know is that using rand() after
it produces seeded random numbers, which are different each time the
program's run,

Only if the seed is different. Also note that 'rand()'
necessarily generates only 'psudo-random' numbers, which
are sufficient for many applications.

Quote:
and it has something to do with the system clock...

No it does not. You're getting confused because often folks
use the system time as the seed. This is simply because it's
the most 'random' value which can be generated with standard
code.

Quote:
can
anyone help me with this one?

This one what?


Quote:
It's quite frustrating to not be able to
fix things since you've no idea what you're supposed to be doing.

What's broken?

-Mike




Back to top
LNK2005
Guest





PostPosted: Thu Mar 04, 2004 9:48 am    Post subject: Re: Why isn't srand working? Reply with quote

"David Shaw" <spamthisemail (AT) mailinator (DOT) com> skrev i meddelandet
news:10cd2710.0403032243.20b4793b (AT) posting (DOT) google.com...
Quote:
On another note,
what exactly does srand() *do*? All I know is that using rand() after
it produces seeded random numbers, which are different each time the
program's run, and it has something to do with the system clock... can
anyone help me with this one?

What it does *exactly* (what algorithm is used) is probably implementation
specific (I think?). Numbers like this aren't really random; rand() will
produce an identical sequence of "random" numbers the next time you call
srand() with the same argument. The system clock has nothing to do with
generating the numbers, it's just that people usually pass the current time
as an argument to srand(), to make rand() more unpredictable. For true
non-deterministic random numbers, you'll need some source of "natural"
randomness, like static noise from a radio, radioactive decay, blob
movements in a lava lamp, etc. See www.random.org for more info.

Just to give a hint what pseudo-random generator algorithms do, here's an
extremely simple (and extremely crappy) one:
- To startup, let randno = seed, where seed is some float between 0 and 1
- To get the next "random" number, let randno = fractional part of (147 *
randno)

However, I'm pretty sure this is *not* how srand() and rand() are typically
implemented;-)



Back to top
Karl Heinz Buchegger
Guest





PostPosted: Fri Mar 05, 2004 2:28 pm    Post subject: Re: Why isn't srand working? Reply with quote

David Shaw wrote:
Quote:


And here's my code:

#include iostream> // I took out the
#include stdlib.h> // in these #includes
#include time.h> // so that they will work on the web.

It would have worked anyway "on the web". Most importantly because
this is *not* the web. This is usenet and we use text only.

More to the point of your post.
Don't mix old style and new style headers. This is a source
of confusion.

#include <iostream>
#include <cstdlib>
#include <ctime>

Quote:

using namespace std;

srand((unsigned)time(NULL));

The above is a function call. But function calls (unless they
are used to initialize some global variables) need to be in some
function!!!

int main()
{
srand((unsigned)time( NULL ));

display();

...
}

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
CBFalconer
Guest





PostPosted: Fri Mar 05, 2004 4:48 pm    Post subject: Re: Why isn't srand working? Reply with quote

Karl Heinz Buchegger wrote:
Quote:
David Shaw wrote:

And here's my code:

#include iostream> // I took out the
#include stdlib.h> // in these #includes
#include time.h> // so that they will work on the web.

It would have worked anyway "on the web". Most importantly because
this is *not* the web. This is usenet and we use text only.

More to the point of your post.
Don't mix old style and new style headers. This is a source
of confusion.

#include <iostream
#include #include

Please do not post c++ code to c.l.c. FUPs set.

--
Chuck F (cbfalconer (AT) yahoo (DOT) com) (cbfalconer (AT) worldnet (DOT) att.net)
Available for consulting/temporary embedded and systems.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.