[ back to toc ]

???

Date: 2002/04/04 09:39

Q:
Hi,

1) I would like to know how to use srand(seed) function. I don't quite
understand how to use it. What do we put in the parameter (seed)? What is
it for?
And what's the different between these two functions: rand() and
srand(seed)?

2) I'm having problem initializing a random number in the array of
structure.
For example,

static struct c_file Text[3]={ {"hello.txt", rand()},
{"hi.txt",rand()},
{"ok.txt",rand()}
}

here, the compiler will say that initializing the Text[0].number is not
constant, so as Text[1].number and Text[2].number.....So, can you please
help me to think another way to initialize a random number in an element
of array of structure?

Please reply soon.
Thanks in advance,
Christina.

A:
Initialization happens when the compiler compiles the code and not during
run time. Therefore in place of the rand() you have to use some
expression, which is constant.

What you really have to do it to write a small code segment that fills up
the array.

regarding the difference of srand() and rand() see the manual. srand seeds
the random number generator, whil rand returns a random number.

Do NOT use the random number generator built in into the C library for
real random numbers though. Use some that you know the algorithm.

srand argument can be any number. Ususally people tend to use the the
actual time value.

Regards,
Peter

[ back to toc ]