Archived

This topic is now archived and is closed to further replies.

C++ and Random Numbers

This topic is 5966 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to create a script not saying random in general, but like random 4. between 0 and 4 only. I am not sure but im guessing I would use rand(4);, since rand() is the ay to create a random number in general? ~ from the depths of the ocean

Share this post


Link to post
Share on other sites
to make it a little more random try this:



int iVar = 0;

srand( time ( 0 ) );
// need to include time.h for this to work
// this just creates a seed according to the time of day

iVar = ( rand() % 5 );



Then you will have a random integer between 0 and 4.




Edited by - -X-Modena-X- on August 10, 2001 3:30:08 PM

Share this post


Link to post
Share on other sites
I included the time header file, and the iostream header file, to include cout so I could test the "randomness" but I got an error. This is how I wrote it.
//upon posting, the iostream.h and time.h dont show, because of
//the html backets, so i put them in () instead
//I didnt do that in the script though.lol
#include(iostream.h)
#include(time.h)

int main()
{

int iVar = 0;

srand( time ( 0 ) );

iVar = ( rand() % 2 );
//I added the next line to test it
cout <<"iVar = "<< iVar <<" .\n";
}

but it is not working. I am just trying to make something that set a bool variable to true or flase, allowing a player to hit the enemy if its true(meaning his swing was accurate) and false(meaning his swing was innacurate)(this is my first battle system in C++)(I am a flash programmer)

quote:
Original post by -X-Modena-X-
to make it a little more random try this:



int iVar = 0;

srand( time ( 0 ) );
// need to include time.h for this to work
// this just creates a seed according to the time of day

iVar = ( rand() % 5 );



Then you will have a random integer between 0 and 4.




Edited by - -X-Modena-X- on August 10, 2001 3:30:08 PM




~ from the depths of the ocean

Edited by - kaiel090x on August 10, 2001 3:59:48 PM

Share this post


Link to post
Share on other sites
  
#include <iostream>
#include <cstdio>
#include <ctime>

using namespace std;

int main ()
{
srand (time (0));
int iVar = rand () % 2;
cout << "iVar = " << iVar << ".\n";
return 0;
}

This program compiles & works fine.

A couple of notes:
- the statement "it doesn''t work" is absolutely useless. If it doesn''t compile, say it doesn''t compile & list the compiler error (and the lines that generate it). If it doesn''t link, say it doesn''t link & list the link error. If it compiles & runs but doesn''t give you the behavior you expect, describe the behavior.
- never use iostream.h. It''s nonstandard & bad news.
- if you declare main to return an int, return an int.
- names in the C++ Standard Template Library (STL) are generally in namespace std. This includes the iostream classes and the cout global stream. You''ll have to specify namespace std to use them. The reason yours was compiling was because you were using hte nonstandard header file, which do not use namespace std.

Share this post


Link to post
Share on other sites
There was another reason it didnt compile(see I said it correctly this time) is because of the compiler I was using. It was Quincy 99, and it was just a badly made compiler. I tested the script you gave me in it and the compiler gave me errors. So I put it in MetroWerks CodeWarrior and it worked fine. Thank you very much for your help.



quote:
Original post by Stoffel

    
#include <iostream>
#include <cstdio>
#include <ctime>

using namespace std;

int main ()
{
srand (time (0));
int iVar = rand () % 2;
cout << "iVar = " << iVar << ".\n";
return 0;
}

This program compiles & works fine.

A couple of notes:
- the statement "it doesn''t work" is absolutely useless. If it doesn''t compile, say it doesn''t compile & list the compiler error (and the lines that generate it). If it doesn''t link, say it doesn''t link & list the link error. If it compiles & runs but doesn''t give you the behavior you expect, describe the behavior.
- never use iostream.h. It''s nonstandard & bad news.
- if you declare main to return an int, return an int.
- names in the C++ Standard Template Library (STL) are generally in namespace std. This includes the iostream classes and the cout global stream. You''ll have to specify namespace std to use them. The reason yours was compiling was because you were using hte nonstandard header file, which do not use namespace std.



~ from the depths of the ocean

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Look for a post in the General Programming section of the forums on random numbers. I explained there why using the % operator to generate a range of random numbers is bad, very bad.

Share this post


Link to post
Share on other sites