Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Random function in C

  1. Feb 7, 2013 #1
    Can someone tell me why this random function is not random? I copied it exactly from my textbook, however, it only generates a "3", rather than a random number (1, 2, or 3).

    PHP:
    int randomMove ()//generates a random move for player 2
    {

        int x = rand () % 3 + 1;
        return x;

    }


    int main() {
        int play = randomMove ();
        cout << play;
        return 0;
    }
     
  2. jcsd
  3. Feb 7, 2013 #2

    I like Serena

    User Avatar
    Homework Helper

    Hi sandy.bridge! :smile:

    It would generate a random value... except that you need to initialize the random generator first...
    To do so, you need a call to srand() with a value that is supposedly random.
    It is still quite a challenge to get an initially random value.
    On linux, the special device /dev/urandom can be used to get it.
    Old school is to just get the time() result and use that as an initial value, but it fails when people world wide initialize their random value in the same second.
     
  4. Feb 7, 2013 #3
    Perfect. Thank you I_Like_Serena! I fixed it as follows,

    PHP:
    int randomMove ()//generates a random move for player 2
    {
        int x;
        for (int i=1; i<=1; i++)
        {
            x = rand () % 3 + 1;
        }
        return x;
    }


    int main() {
        srand(time(0));
        int play = randomMove ();
        cout << play;
        return 0;
    }
     
     
  5. Feb 7, 2013 #4

    I like Serena

    User Avatar
    Homework Helper

  6. Feb 7, 2013 #5
    One more thing! Can you use expresssions with AND/OR statements in C? For example, can I go (A==1 && B==3)||(A==2 && B==1)?
     
  7. Feb 7, 2013 #6

    I like Serena

    User Avatar
    Homework Helper

    Of course you can. That is exactly what C/C++ is good and efficient at.
    I'm not sure if I understand you holding back on it.
     
  8. Feb 7, 2013 #7
    To be pedantic...
    Keep in mind, that rand() function is pseudo-random number generator.
    Mathematically it represents an iterator moving over predefined sequence.
    srand() is just a moving to specific point of this sequence.
    It's good for debug purposes, but shouldn't be used where true entropy is needed
     
  9. Mar 1, 2013 #8
    In regards to the random function. Why is it when I include the random function in this loop, each time it passes through the loop a new value for x is not given? It is generating an initial random number, then all of the following numbers are the same.


    PHP:

    int randomDraw(int number)
    {   srand(time(0));
        int x;
        x=rand() % number + 1;

        return x;

    }
    ....
            while (i < r)
        {
            x=randomDraw(r);
            ball[i]=x;
            cout << ball[i] << " ";
            i = i + 1;
        }
     
     
    Last edited: Mar 1, 2013
  10. Mar 1, 2013 #9

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    You haven't shown us the code of randomDraw(), so we have no idea why it doesn't work.
     
  11. Mar 1, 2013 #10
    Whoops, that was supposed to be in there. I just added it.
     
  12. Mar 1, 2013 #11

    jedishrfu

    Staff: Mentor

    What does the time(0) function do?

    If it always returns the same value then you are always initializing the random number generator with the same seed which is gaur ranted to give the same sequence of random numbers that how people test their programs.

    The next problem I see is that you are calling srand() each time you call randomDraw. This is wrong you only need to call srand once and then things are initialized correctly.
     
  13. Mar 1, 2013 #12
    All I had to do was move srand(time(0)) to the body of main for it to work
     
  14. Mar 1, 2013 #13

    jedishrfu

    Staff: Mentor

    Yay...
     
  15. Mar 2, 2013 #14
    Your sarcasm is appreciated! . . . . . .. . ...
     
  16. Mar 5, 2013 #15
    Okay. I need a little bit of help. I am trying generate a random array with numbers 1-k, where k is inputed by the user. I have used a function to do so, and it is supposed to return the ith element in the ray such that array=array[0]. It can be compared to drawing a random colored ball from a jar, then putting it back. Than you start counting until you draw that same ball again.

    When I cout the function, it doesn't seem to output what i turns out to be. Any suggestions is greatly appreciated.

    PHP:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>

    using namespace std;

    int random(int k)
    {       int i = 0;
            int array[100];
            while(i<100){
            array[i] = rand()%k;
            int y = array[0];
            cout << array[i] << " ";
            if (i>0 && y==array[i]){return i;}
            else {i++;}
            }
    }


    int main() {
        int i, j, k, l, sum = 0;

        srand(time(0));
        cout << "Please enter the number of colours: ";
        cin >> k;
        cout << "Please enter the number of trials: ";
        cin >> j;

        random(k);
        cout << endl << random(k);




        return 0;
        }
     
    I have not used all the variables I have in this code yet, but here is what I get in the console:
    PHP:
    Please enter the number of colours: 50
    Please enter the number of trials: 50
    44 1 48 37 27 5 36 27 36 3 15 11 45 35 2 37 32 23 18 19 34 15 25 26 19 41 18 6 40 37 48 10 42 35 48 7 45 28 11 41 49 0 24 27 30 45 24 11 10 35 37 11 10 26 16 28 32 38 49 49 23 28 17 9 27 10 49 34 9 31 25 17 21 48 5 45 17 12 13 27 14 24 44 16 45 27 24 18 19 44 35 34 32 45 9 1 20 0 39 11 37 47 34 36 39 16
    22
     
  17. Mar 5, 2013 #16

    I like Serena

    User Avatar
    Homework Helper

    Hi sandy.bridge!

    Your code looks okay at first glance... need more info...

    Suppose you entered 5 for the number of colours, then your program should output a value that should be around 5.
    Doesn't it?
     
  18. Mar 5, 2013 #17
    Nope. This is what happens when I use 5.
    PHP:
    Please enter the number of colours: 5
    Please enter the number of trials: 5
    1 2 0 1 3 2 3
    2
    i should have been 3.
     
  19. Mar 5, 2013 #18

    I like Serena

    User Avatar
    Homework Helper

    That looks about right.

    First you execute random(50) which prints 83 numbers without you doing anything with it.
    It breaks of when at index 83 the number 44 is repeated.
    Then you execute random(50) again with a first number of 16, and on index=22your program breaks off since it found 16 again.

    So what's the problem? :tongue:
     
  20. Mar 5, 2013 #19

    I like Serena

    User Avatar
    Homework Helper

    Same thing. The first time it was 3.
    But then you went on, and the second time it was 2 (when the number 3 was repeated).
     
  21. Mar 5, 2013 #20
    Wouldn't the function terminate the first time that it happens? I thought that was how the return worked.

    Is there a way for me to have to array cease to build the moment that array=array[0]? I made the array have 100 items just to ensure the number would for sure be duplicated.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Random function in C
  1. Functions in c (Replies: 1)

  2. Random function in c++ (Replies: 6)

  3. C++ random numbers! (Replies: 3)

Loading...