Hi,
i want to get random number between 1-100.
i tried with rand(100) and the compiler dont recognize it.
i added #include <stdlib.h>.
please help.
Or.
Hi,
i want to get random number between 1-100.
i tried with rand(100) and the compiler dont recognize it.
i added #include <stdlib.h>.
please help.
Or.
Try
Code:#include "stdafx.h" #include "time.h" #include "stdlib.h" float random; srand(time(NULL)); random=(rand()%101);
Why not only random=rand()%100;
working too.
another question, maby you can help me with this.
i have a struct like this:
how can i initialize the struct qucikly.Code:struct job { char Name[MAXNAME]; int Proirity; };
i mean something like that : job arr[10];
thanks.Code:arr[] = { {"name",3} {"name2",6} };
Or.
You use the srand() call to seed the random number generator... it will always give you the same sequence if you don't.
The array initialization will be compiler dependent. Compilers before C-99 may or may not accept parantetic initialization, but I believe most C-99 compliant ones do.
You can't assign directly to an array after you create it, but you can use an initializer (which means it has to be done at the point you create your array) as you have it, and it's valid C89 and C99:
To assign to the array later, after it's been initialized, you'll have to do something like:Code:struct job { char name[64]; int pri; } arr[10] = { { "name1", 3 }, { "name2", 10 }, };
You could use other methods to “assign” to an array later on, such as:Code:strcpy(arr[2].name, "name3"); arr[2].pri = 5;
This will replace all the contents of your old array with the new one (which only has one element set). But whether that's better or not... well, that's up to you to decide.Code:const struct job otherjobs[sizeof arr] = { { "othername", 5 } }; memcpy(arr, otherjobs, sizeof arr);
Thanks Alot!
That's a very broken example!
The correct calculation would beotherwise you get a number from 0 to 100 when he asked for a number from 1 to 100. 'random' should also be int, not float.Code:random = rand() % 100 + 1;
You forgot to say that srand should only be called once for the whole program and not once per random number.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"