rand()

J

jw

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

Stephen M. Webb

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

--smw
 
K

Kristo

jw said:
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
 
J

jw

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

Rolf Magnus

jw said:
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
}
}
 
S

Stephen M. Webb

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
 
H

Howard

jw said:
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
 
J

jw

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
 
W

W Marsh

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.
 
H

Howard

jw said:
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
 
J

jw

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();
}
 
H

Howard

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
 
D

Default User

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
 
V

Vladimir

jw said:
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!
 
P

Pete Becker

Stephen said:
(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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top