kanang mangayu unta kog tabang ani:
-unsaon pag generate ug random questions if for example naa kay game nga program like who wants to be a millonaire or unsa.
-dayun asa man kuha ang function nga mu randomize sa mga questions nga dapat i randomize?
kanang mangayu unta kog tabang ani:
-unsaon pag generate ug random questions if for example naa kay game nga program like who wants to be a millonaire or unsa.
-dayun asa man kuha ang function nga mu randomize sa mga questions nga dapat i randomize?
murag naa man rand() or randomize() nga function bro. kaw lng set sa limit. basta dali ra kaayo na bro.![]()
ahhh..nakadungog sad ko ana bro pero ang ako bro bah, unsaon man pag randomize sa angay i randomize
ako idea ana kay silbi naay koy usa ka library sa mga questions then mao i call sa randomize..
mao ako idea,pero mangayu unta kog example bah.
Butangi lag ID ang kada question sa imo Library baii...Originally Posted by chokobo
Then kanang ID which is an int integer maoy imo i pass sa randomize nga function....
Then imo i pair ang ID sa kada question....
Just an idea lang ni ako...
sayon ra na bro, example you have 5 questions. di nako kahinumdum sa exact nga pag gamit pero sabta nlng...
mag randomize kag 5 integers. so you can generate integers 1 - 5 in random. then imu questions ibutang nlng nimu ug switch or if statements. from there, mkagenerate na kag ramdom questions.![]()
gae daw kog concrete example bai, bisan mga 3 questions lang or bisan murag scratch
you can also replace your switch statements with tables instead (implemented as an array of structures)
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
void seed_random();
int get_random(int);
main()
{
Â* seed_random(); /* note:dont seed the random function everytime you call it */
Â* int random_no = get_random(100);
Â* printf("%d ", random_no);
Â* return 0;
}
void seed_random()
{
Â* time_t t1;
Â* (void) time(&t1);Â*
Â* srand((long) t1); /* use time to set seed */Â*
}
int get_random(int limit)
{
Â* return rand() % limit;
}
here's a code to generate random numbers using the system time as seed.
as what almost everyone here said, assign an integer id to each of your question. that would be from 0 to the number of your questions minus 1.
you can put your questions with the ids in a database or in a file or anywhere you want for persistence as long as you have the means to query a particular question using its designated id.
use the get_random to get a random number and then use the random number to query the question. dont forget to pass the total number of questions as the argument for the get_random function.
note: seed the random function only once. typically, during startup of the application. unless kailangan jud nimo magsige ug seed, which i really dont think you do.
good luck bay! kaya ra na nimo uy! :mrgreen:
ah thanks bai pero la pako knowledge sa seed
ako nlng ghimuan ug lain paagi
wa nlng nako gi random
thanks ninyo!
ahh ok.. cge bay good luck! :mrgreen:
Similar Threads |
|