Computing.Net > Forums > Programming > Random numbers

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Random numbers

Reply to Message Icon

Name: RogueStar
Date: November 2, 2003 at 15:56:59 Pacific
OS: WinXP
CPU/Ram: Duron 1.2 /384
Comment:

I'm new to C++ programming, and I've discovered the need for a random number generator. What would be the best way to go about this? In this case, I won't need a number more than 30 or so



Sponsored Link
Ads by Google

Response Number 1
Name: RogueStar
Date: November 2, 2003 at 16:02:16 Pacific
Reply:

Sorry, I'm using VC++ 6, with out bothering with creating windows at this point.


0

Response Number 2
Name: Rolos
Date: November 2, 2003 at 18:41:34 Pacific
Reply:

You would need to use the modulus operator for random numbers in C++. So you would do something like:

rand() % 30;

That would give you a random number from 0 to 30. Hope this helps.

- Rolos


0

Response Number 3
Name: JasonR
Date: November 3, 2003 at 04:44:01 Pacific
Reply:

Small correction to last post

rand() % 30

will give numbers between 0 and 29


0

Response Number 4
Name: Rolos
Date: November 3, 2003 at 16:29:53 Pacific
Reply:

Jason: Thanks for catching that, you are absolutely right.

Rogue Star: What you would do to accomodate for this is to just add a one to the expression as such:

int someVariable;
someVariable = ((rand() % 50) + 1);

You may also want to do some exception handling if you want to implement 0's that are returned from the function call into your program. But it is dependant on your implementation and I won't go into it.

I hope that I have answered your question and good luck with your project/assignment.

- Rolos


0

Response Number 5
Name: RogueStar
Date: November 4, 2003 at 13:26:04 Pacific
Reply:

Thanks for the help, gang. Dearly appreciated. That should be enough for me to proceed.
Currently working on a text adventure for a sort of "final project" as I work my way through to the end of the book. Anybody want to have a look after it's done, I'll make it available for testing


0

Related Posts

See More



Response Number 6
Name: Iuri Cernov
Date: November 6, 2003 at 13:23:40 Pacific
Reply:

Before use the rand() function, you need this function:

// in DOS or Unix
void randomize(void) {
srand((unsigned)time(NULL));
}

// in Unix
void randomize(void) {
FILE *fl = fopen('/dev/random', 'rb');
unsigned short i;
if (fl == NULL) return;
fread(&i, 2, 1, fl);
srand(i);
// fclose(fl) // do not need!
}


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Random numbers

Unique random numbers www.computing.net/answers/programming/unique-random-numbers/1278.html

C++ Random numbers www.computing.net/answers/programming/c-random-numbers-/8038.html

Large Random Numbers in C++ www.computing.net/answers/programming/large-random-numbers-in-c/11992.html