I'm trying to port some code written in MS Visual C++ 8.0 (the version is copyright 1998, I know that for sure.) into MS Visual C++ 2005 Express. I've been having trouble trying to translate the code though.

I've been using #include<random> as a header to generate random numbers. After doing some reading, I've discovered that, these days, #include<cstdlib> is now used to produce random numbers.

I'm posting to require some help translating my randomized variables.

So;

generating random variables between 1 and 100 using #include<random>

randomize();
        int num1, num2;
        num1=1+random(100);
        num2=1+random(100);

should become

int num1 = rand() % 100 + 1;
int num2 = rand() % 100 + 1;

Right? (I know there are probably some cleaner ways of declaring those variables :] )

Is there anything else I should know in order to avoid any complications while using #include<cstdlib>?

Any help or advice would be much appreciated! Thanks!

>I'm trying to port some code written in MS Visual C++ 8.0 (the version is copyright 1998, I know that for sure.)

Are you so sure about that? Perhaps you meant version 6.0. That has a copyright of 1999 (at least the version that I had).

>these days, #include<cstdlib> is now used to produce random numbers.
For using rand(), yes you need cstdlib . In plain-old C you would use stdlib.h .

>Right?
That's one way of doing it, yes. For a few random numbers in a little game that is perfectly adequete. However using the modulos operator actually brings down rand()'s randomness, so if you want to learn how to use rand() for better randomness, I suggest reading Narue's excellent rand tutorial:

http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx

Thanks again, joeprogrammer.

I'm only using two random numbers since it's a small math drill program that I'm writing. I'm in the middle of reading that tutorial though.

Also, you're right about the C++ version. I couldn't remember if it was 6.0 or 8.0, only the copyright date.

Thanks again for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.