rand()

Discussion in 'C++' started by jw, Nov 28, 2005.

  1. jw

    jw Guest

    hi all;
    i have a function like this,
    it always rands the same number and gives error if i take the srand
    function out of the for loop
    void list::getWords()
    { int num;

    for(int m=0;m<20;m++)
    {
    srand((unsigned)time( NULL ));
    num=(rand()%41)-1;
    node *ptr;
    ptr=new node;
    ptr->word=wordsall[num];
    append(ptr);//appends to the linked list
    }
    }
    jw, Nov 28, 2005
    #1
    1. Advertising

  2. Why reseed the random number generator on each iteration? That's
    pretty much guaranteed to give you a nonrandom distribution.

    --smw
    Stephen M. Webb, Nov 28, 2005
    #2
    1. Advertising

  3. jw

    Kristo Guest

    jw wrote:
    > hi all;
    > i have a function like this,
    > it always rands the same number and gives error if i take the srand
    > function out of the for loop
    > void list::getWords()
    > { int num;
    >
    > for(int m=0;m<20;m++)
    > {
    > srand((unsigned)time( NULL ));
    > num=(rand()%41)-1;
    > node *ptr;
    > ptr=new node;
    > ptr->word=wordsall[num];
    > append(ptr);//appends to the linked list
    > }
    > }


    You only need to call srand once, so moving it out of the for-loop is a
    better place for it. What's the compiler error you're getting?

    Also, next time please post complete, minimal, *compilable* code per
    FAQ 5.8.

    Kristo
    Kristo, Nov 28, 2005
    #3
  4. jw

    jw Guest

    if i take it out of the loop it crashes while running,but now it doesnt
    crash but always rands the same number
    jw, Nov 28, 2005
    #4
  5. jw

    Rolf Magnus Guest

    jw wrote:

    > hi all;
    > i have a function like this, it always rands the same number


    That's because srand initializes the random number generator, and you
    re-initialize it in each loop iteration.

    > and gives error if i take the srand function out of the for loop


    What do you mean by "an error"? Where did you put the call? What exactly
    does the error message say? In which line?

    > void list::getWords()
    > { int num;
    >
    > for(int m=0;m<20;m++)
    > {
    > srand((unsigned)time( NULL ));
    > num=(rand()%41)-1;
    > node *ptr;
    > ptr=new node;
    > ptr->word=wordsall[num];
    > append(ptr);//appends to the linked list
    > }
    > }
    Rolf Magnus, Nov 28, 2005
    #5
  6. num=(rand()%41)-1;
    ptr->word=wordsall[num];

    Aside from the lack of randomness of your generated numbers (rand()
    fails tests for randomness in its little bits), using a negative number
    as an array index is probably not what you want.

    --smw
    Stephen M. Webb, Nov 28, 2005
    #6
  7. jw

    jw Guest

    not in a line the program crashes and stops running if i take it out
    of the loop
    jw, Nov 28, 2005
    #7
  8. jw

    Howard Guest

    "jw" <> wrote in message
    news:...
    > not in a line the program crashes and stops running if i take it out
    > of the loop
    >


    Please quote the message you're responding to, ok? It's difficult for us to
    know what you're talking about given just that one line response.

    But given this line of code:

    num=(rand()%41)-1;

    ....chances are that at some point the value of num becomes -1. That's not a
    valid index for the line:

    ptr->word=wordsall[num];

    This might be the cause of the crash.

    -Howard
    Howard, Nov 28, 2005
    #8
  9. jw

    jw Guest

    yeah i think the problem is there it gives a negative number but it
    must not give a negative number because if we take the mod of a number
    it doesnt give 0
    jw, Nov 28, 2005
    #9
  10. jw

    W Marsh Guest

    On 28 Nov 2005 09:28:41 -0800, "jw" <> wrote:

    >yeah i think the problem is there it gives a negative number but it
    >must not give a negative number because if we take the mod of a number
    >it doesnt give 0


    Yes it does. 4%2, for example. I don't think you understand what
    you're doing - read through your tutorials more carefully.
    W Marsh, Nov 28, 2005
    #10
  11. jw

    Howard Guest

    "jw" <> wrote in message
    news:...
    > yeah i think the problem is there it gives a negative number but it
    > must not give a negative number because if we take the mod of a number
    > it doesnt give 0
    >


    (You're still not including the text you're responding to! (Hint: don't use
    the Reply button to reply. I don't use Google, but I think you can reply
    using some drop-down menu at the top?)

    Why do you say taking the mod of a number does not return 0? It most
    certainly does, whenever the value you're taking the mod of is equal to a
    multiple of the divisor. For example, 8 mod 4 is 0. The modulus function
    gives you value from 0 through n-1.

    In your sample, every time the result of rand gives you a multiple of 41,
    then taking that value mod 41 will result in 0. Subtracting 1 gives you -1.

    -Howard
    Howard, Nov 28, 2005
    #11
  12. jw

    jw Guest

    here is the whole code;
    #include<iostream>
    #include<fstream>
    #include<vector>
    #include<string>
    #include<ctime>
    using namespace std;
    #ifndef NULL
    #define NULL (short) 0
    #endif

    char
    *words[40]={"come","go","school","nice","united","college","apple","orange","old","days","who","knows"};

    //*******************node clasim*******************
    class node{
    private:
    string word;
    node *next;
    public:

    node()
    {
    next=NULL;
    }

    friend class list;

    void display()
    {
    cout<<word<<" ";
    }
    };

    //*****************list clasim*********************
    class list{
    private:
    node *head,*tail,*TOP;
    void append(node *new_node);

    public:
    list()
    {
    TOP=new node;
    head=tail=TOP;
    TOP->next=NULL;
    }
    //~list()delete...
    void getWords();
    void displayAll();

    };


    void list::displayAll()
    {

    node *py;

    py=head;

    if(py==NULL)
    {
    cout<<"nothing to show"<<endl;
    }
    while(py!=NULL){//while py points sth reasonable

    py->display();
    py=py->next;
    }

    }

    void list::append(node *ptr){

    if(TOP->next==NULL)//first element
    { TOP->next=ptr;
    head=ptr;
    tail=ptr;
    }
    else
    {

    tail->next=ptr;
    tail=ptr;

    }

    }

    void list::getWords()
    { int num;
    srand((unsigned)time( NULL ));
    for(int m=0;m<20;m++)
    {

    num=(rand()%41);

    node *ptr;
    ptr=new node;
    ptr->word=words[num];
    append(ptr);
    }
    }




    void main()
    {
    list thelist;
    thelist.getWords();
    thelist.displayAll();
    }
    jw, Nov 28, 2005
    #12
  13. jw

    Howard Guest

    "jw" <> wrote in message
    news:...

    > char
    > *words[40]={"come","go","school","nice","united","college","apple","orange","old","days","who","knows"};


    I see only 12 words there.

    >
    > num=(rand()%41);


    Why 41? (See below)

    >
    > node *ptr;
    > ptr=new node;
    > ptr->word=words[num];
    > append(ptr);


    Here you're appending words from index positions 0..40. Ask yourself these
    things:

    1) what about all those words after the 12th one? Did you want 40 words?
    Or a smaller array?

    2) what happens when num == 40? What does words[40] mean when you declared
    words as char* [40]?

    -Howard
    Howard, Nov 28, 2005
    #13
  14. jw

    jw Guest

    thanks,am so sorry for such a silly mistake forgive me all
    jw, Nov 28, 2005
    #14
  15. jw

    Default User Guest

    Howard wrote:


    > (You're still not including the text you're responding to! (Hint:
    > don't use the Reply button to reply. I don't use Google, but I think
    > you can reply using some drop-down menu at the top?)


    See my .sig for details.



    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Nov 28, 2005
    #15
  16. jw

    Vladimir Guest

    jw wrote:
    > i have a function like this,
    > it always rands the same number and gives error if i take the srand
    > function out of the for loop


    If taking srand out gives an error then for god's sake don't do it
    because teacher will punish you for any errors!

    --
    Vladimir
    Vladimir, Nov 28, 2005
    #16
  17. jw

    Pete Becker Guest

    Stephen M. Webb wrote:
    >
    > (rand()
    > fails tests for randomness in its little bits)


    Where in the standard is this requirement? <g>

    Of course, this may be true of some implementations of rand, and it's
    certainly part of the lore of rand, but that doesn't make it universally
    true.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Nov 30, 2005
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Niko D. Barli

    rand function in Modelsim 5.7c

    Niko D. Barli, Aug 26, 2004, in forum: VHDL
    Replies:
    9
    Views:
    6,289
    Niko D. Barli
    Sep 6, 2004
  2. daniel kaplan

    rand() question

    daniel kaplan, Sep 15, 2004, in forum: Perl
    Replies:
    4
    Views:
    620
    Ian Sedwell
    Sep 21, 2004
  3. Amelyan

    what is rand?

    Amelyan, Mar 31, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    532
    Kevin Spencer
    Mar 31, 2006
  4. Orhan Demirel

    usage of rand()

    Orhan Demirel, Jul 21, 2003, in forum: C++
    Replies:
    1
    Views:
    388
    Adam Fineman
    Jul 21, 2003
  5. 7stud --

    rand() v. rand(0.1) ?

    7stud --, Sep 15, 2007, in forum: Ruby
    Replies:
    6
    Views:
    218
    Morton Goldberg
    Sep 16, 2007
Loading...

Share This Page