473,511 Members | 14,975 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rand()

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

Nov 28 '05 #1
16 2827
Why reseed the random number generator on each iteration? That's
pretty much guaranteed to give you a nonrandom distribution.

--smw

Nov 28 '05 #2
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

Nov 28 '05 #3
jw
if i take it out of the loop it crashes while running,but now it doesnt
crash but always rands the same number

Nov 28 '05 #4
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
}
}


Nov 28 '05 #5
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

Nov 28 '05 #6
jw
not in a line the program crashes and stops running if i take it out
of the loop

Nov 28 '05 #7

"jw" <ja*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
Nov 28 '05 #8
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

Nov 28 '05 #9
On 28 Nov 2005 09:28:41 -0800, "jw" <ja*****@gmail.com> 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.
Nov 28 '05 #10

"jw" <ja*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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
Nov 28 '05 #11
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();
}

Nov 28 '05 #12

"jw" <ja*****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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

Nov 28 '05 #13
jw
thanks,am so sorry for such a silly mistake forgive me all

Nov 28 '05 #14
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.
Nov 28 '05 #15

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

Nov 28 '05 #16
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)
Nov 30 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4188
by: Tom McCallum | last post by:
Can any tell me why rand() is such a bad pseudo-random number generator. In all the articles I have read they say that you can predict the outcome of rand() but when I used its output with NIST's...
7
3233
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
36
7082
by: Ben Justice | last post by:
For a program in c, I need some random numbers for a system were people are placing bets. This is not a commerical project btw. Generally, I tend to rely on things from the standard library,...
36
2649
by: Profetas | last post by:
Hi, I want to generate a random 8 bit number using rand(0 is that possible? to expecifu the base and the lenght? thanks
8
4378
by: Jack | last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that the same value will not appear twice? And is this a floating window? For example, if RAND_MAX is 32767, and I make...
4
2782
by: Siam | last post by:
Hi all, I'm writing a shell language in c++ that supports the generation of random numbers, and by its nature, each command must be executed in a new thread. The call to the random function from...
13
3645
by: Spiros Bousbouras | last post by:
The standard says that rand() should return a pseudo-random number but what does pseudorandom mean ? If an implementation of rand() always returned the same number would it be conforming ? What if...
5
2308
by: ds | last post by:
Hi all, rand() is not thread safe, a fact that may not be so bad after all.. However, I face the following problem: a piece of code uses rand() to get a random sequence, but always seeds with...
10
2994
by: Rafael Cunha de Almeida | last post by:
Hi, I've found several sites on google telling me that I shouldn't use rand() % range+1 and I should, instead, use something like: lowest+int(range*rand()/(RAND_MAX + 1.0))
15
3915
by: Rich Fife | last post by:
Quick rand() question: I know you're not supposed to use "rand() % 1024" for instance, because it focuses on the lower bits. However, it seems to me that given that the argument is not a power...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.