C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

rand()
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
jw
Guest





PostPosted: Mon Nov 28, 2005 4:59 pm    Post subject: rand() Reply with quote



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
}
}

Back to top
Stephen M. Webb
Guest





PostPosted: Mon Nov 28, 2005 5:06 pm    Post subject: Re: rand() Reply with quote



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

--smw

Back to top
Kristo
Guest





PostPosted: Mon Nov 28, 2005 5:09 pm    Post subject: Re: rand() Reply with quote



jw wrote:
Quote:
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


Back to top
jw
Guest





PostPosted: Mon Nov 28, 2005 5:13 pm    Post subject: Re: rand() Reply with quote

if i take it out of the loop it crashes while running,but now it doesnt
crash but always rands the same number

Back to top
Rolf Magnus
Guest





PostPosted: Mon Nov 28, 2005 5:17 pm    Post subject: Re: rand() Reply with quote

jw wrote:

Quote:
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.

Quote:
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?

Quote:
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
}
}


Back to top
Stephen M. Webb
Guest





PostPosted: Mon Nov 28, 2005 5:17 pm    Post subject: Re: rand() Reply with quote

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

Back to top
jw
Guest





PostPosted: Mon Nov 28, 2005 5:19 pm    Post subject: Re: rand() Reply with quote

not in a line the program crashes and stops running if i take it out
of the loop

Back to top
Howard
Guest





PostPosted: Mon Nov 28, 2005 5:25 pm    Post subject: Re: rand() Reply with quote


"jw" <jackwht (AT) gmail (DOT) com> wrote

Quote:
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



Back to top
jw
Guest





PostPosted: Mon Nov 28, 2005 5:28 pm    Post subject: Re: rand() Reply with quote

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

Back to top
W Marsh
Guest





PostPosted: Mon Nov 28, 2005 5:31 pm    Post subject: Re: rand() Reply with quote

On 28 Nov 2005 09:28:41 -0800, "jw" <jackwht (AT) gmail (DOT) com> wrote:

Quote:
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.

Back to top
Howard
Guest





PostPosted: Mon Nov 28, 2005 5:34 pm    Post subject: Re: rand() Reply with quote


"jw" <jackwht (AT) gmail (DOT) com> wrote

Quote:
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



Back to top
jw
Guest





PostPosted: Mon Nov 28, 2005 5:34 pm    Post subject: Re: rand() Reply with quote

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< }
};

//*****************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"< }
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();
}

Back to top
Howard
Guest





PostPosted: Mon Nov 28, 2005 5:39 pm    Post subject: Re: rand() Reply with quote


"jw" <jackwht (AT) gmail (DOT) com> wrote


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

I see only 12 words there.

Quote:

num=(rand()%41);

Why 41? (See below)

Quote:

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






Back to top
jw
Guest





PostPosted: Mon Nov 28, 2005 5:42 pm    Post subject: Re: rand() Reply with quote

thanks,am so sorry for such a silly mistake forgive me all

Back to top
Default User
Guest





PostPosted: Mon Nov 28, 2005 8:11 pm    Post subject: Re: rand() Reply with quote

Howard wrote:


Quote:
(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.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.