Jump to content

Rand() in c++


Daniel W.
 Share

Recommended Posts

Hi,

i am learning C++ at the moment and i just started 2 days ago.

And i got i question about the rand() function

I got this c++ code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    unsigned int keim;
    int z1,z2,z3;

    cout << "Put in your number: ";
    cin >> keim;

    srand(keim);

    z1 = rand();
    z2 = rand();
    z3 = rand();

    cout << "Your numbers: "
         << z1 << "\t" << z2 << "\t" << z3 << endl;
    return 0;
}

and i always get the same numbers out and i don't know why

i translated the code in autoit

$box = InputBox("", "Your number?")

$1 = Round(Random( 0, $box))
$2 = Round(Random( 0, $box))
$3 = Round(Random( 0, $box))

MsgBox(0, "", $1 & @CRLF & $2 & @CRLF & $3)

but why do i get the same numbers in c++ and always different in autoit?

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

The way you use srand makes rand return the same numbers all tyhe time. Random generators are not "random". The randomnes is created from a function using initialization values and then time to generate a semingly random number. People have written entier books about the topic so don't despaire :)

Take a look at the page @JdeB provided. You could also take a look at this page. And as you are in the very begining of your C/C++ studies I would recomend to take a look at the "Thinking in C++" books by Bruce Eckel. If I recall right one of the projects in the book is a random generator.

Link to comment
Share on other sites

The random number generator that comes with C/C++ is pretty simple; it is just a two-statement function. The one I put in AutoIt is a more advanced generator. Take a look at the Random() function documentation for more details. Also, when AutoIt starts, it calls the srand equivilant for the generator that we are using, so you should not get the same sequence of numbers each time, as you noted.

Edit: Fox spelling error.

Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Here is some C code, using seeds srand() via gettimeofday(). This way, you will always get a different set of random numbers, from 0 to 254. It compiles under Linux and am not sure about Windows. I hope this helps anyway.

-John

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

int
main
(int argc, char *argv[]) {
        struct timeval t;
        struct timezone z;
        int i;

        gettimeofday(&t, &z);
        srand( t.tv_usec );

        for(i=0; i < 5; i++) {
                printf("%d ", rand() % 255);
        }
        printf("\n");


        return 0;
}
Link to comment
Share on other sites

I had the same question when i started to learn c++. Play with this code a little:

#include <ctime>
#include <iostream>

using namespace std;

int main()
{
      srand(time(NULL)); //seeds the random number generator.
      int r = (rand() % 10) + 1; //Generates a random number 1 through 10.
      cout << r;
return 0;
}

i think this code is right... lol

btw, a good way to get started is getting an "in easy steps" book called, "c++ programming", its really good. I started with that book then got a better book called SAMS teach yourself c++ in 24 hours.

The SAMS book is: 502 pages

The "in easy steps" book is: 192 pages.

Edited by CHRIS95219
Link to comment
Share on other sites

Well thank you all

I am german and i already got a book.

Its called "C++ lernen und professionell anwenden" in german, in english it would be

"C++ learning and using it professionell"

:)

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...