מספרים רנדומליים בשפת C - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

מספרים רנדומליים בשפת C


The-Devil

Recommended Posts

הינה מספר דוגמאות למימוש פונקציות/שגרות הרנדומליות בשפת C:

#include <stdlib.h>

#include <stdio.h>

#include <time.h>

/* prints a random number in the range 0 to 99 */

int main(void)

{

randomize();

printf("Random number in the 0-99 range: %d\n", random (100));

return 0;

}

/* ------------------------------------------------------------- */

#include <stdlib.h>

#include <stdio.h>

#include <time.h>

int main(void)

{

int i;

randomize();

printf("Ten random numbers from 0 to 99\n\n");

for(i=0; i<10; i++)

printf("%d\n", rand() % 100);

return 0;

}

/* ------------------------------------------------------ */

#include <stdlib.h>

#include <stdio.h>

#include <time.h>

int main(void)

{

int i;

time_t t;

srand((unsigned) time(&t));

printf("Ten random numbers from 0 to 99\n\n");

for(i=0; i<10; i++)

printf("%d\n", rand() % 100);

return 0;

}

קישור לתוכן
שתף באתרים אחרים

כדאי להזכיר שהקשר בין הפונקציה random או randomize שהשתמשו פה לC הוא מקרי בהחלט. זה לא נמצא באף תקן שמכבד את עצמו, ואני ממליץ להמנע משימוש בפונקציות כאלה שיגבילו אותך בעתיד.

כדאי להשתמש בדרך של צביקה (אפשר להשתמש בזמן יותר מדוייק אם צריך).

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...