#### Archived

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

# C++ and Random Numbers

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

## 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 on other sites

Between 0 and 4 inclusive:

num = rand()%5;

##### 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 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 on other sites
  #include #include #include 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 on other sites
Perhaps you might want to include stdlib.h where rand and srand are declared?

##### 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 #include #include 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 on other sites
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.

• 11
• 20
• 12
• 10
• 38
• ### Forum Statistics

• Total Topics
631400
• Total Posts
2999862
×
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.16730213165283203, "perplexity": 5864.112932685637}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2018-26/segments/1529267864391.61/warc/CC-MAIN-20180622104200-20180622124200-00089.warc.gz"}