There is an easy way to print multiple items from an array in C. The example below shows a simple program that uses the sleep command and will print 5 random items from an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | /******************************************************************** * Description: Random image program. * Author: bejiitas * Created at: Tue Apr 17 02:08:47 EDT 2007 * Computer: opensuse * System: Linux 2.6.20.2-Lorien on i686 * * Copyright (c) 2007 bejiitas_wrath All rights reserved. * ********************************************************************/ /* * This is how I generated this list in the first place... * for file in *.jpg; do echo -ne "\"$file\"," >> ~/out.txt; done; */ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #define foreach(item, array) for(\ int keep = 1, count = 0, size = sizeof (array) / sizeof *(array);\ keep && count != size;\ keep = !keep, count++) for(item = (array) + count;\ keep; keep = !keep\ ) const char* files[] = { "[art] Claude Monet - Bordighera 1884 (2900x2318).jpg", "[art] Claude Monet - Charing Cross Bridge (1903) (2916x2313).jpg", "[art] Claude Monet - Japanese Bridge, Waterlily Pond (Water Irises..) (1777x2403).jpg", "SpliceLKRG.avi_snapshot_01.17.16_[2010.09.09_06.10.02].jpg", "splitting-red.jpg", "spring-tulip-4.jpg", "square-orbs-7a.jpg", "Stagelight_1024x768 (4-3).jpg","Stagelight_1152x864 (4-3).jpg", "Stagelight_1280x1024 (5-4).jpg", "Stagelight_1280x720 (16-9).jpg", "Stagelight_1280x768 (15-9).jpg","Stagelight_1280x800 (16-10).jpg", "Stagelight_1280x960 (4-3).jpg", "Stagelight_1360x768 (16-9).jpg", "Stagelight_1440x900 (16-10).jpg", "Stagelight_1600x900 (16-9).jpg", "Stagelight_1680x1050 (16-10).jpg", "Star Wars - The Clone Wars.jpg", "Star Wars - The Clone Wars.jpg", "Star Wars - The Clone Wars.jpg", "Star Wars - The Clone Wars.jpg", "Star Wars - The Clone Wars.jpg", "Star Wars - The Clone Wars.jpg", "Star Wars - The Clone Wars.jpg", "Star Wars - The Clone Wars.jpg", "Star Wars - The Clone Wars.jpg", "Star Wars - The Clone Wars.jpg", "Star Wars - The Clone Wars.jpg", "Star Wars - The Clone Wars.jpg", "Star.Wars.The.Clone.Wars.jpg", "Star.Wars.The.Clone.Wars..jpg", "Star.Wars.The.Clone.Wars.jpg", "Star.Wars.The.Clone.Wars..jpg", "Star.Wars.The.Clone.Wars.jpg", "Star.Wars.The.Clone.Wars..jpg", "star.wars.the.clone.wars..jpg", "star.wars.the.clone.wars.jpg", "star.wars.the.clone.wars..jpg", "star.wars.the.clone.wars.jpg", "star.wars.the.clone.wars..jpg", "star.wars.the.clone.wars.jpg", "star.wars.the.clone.wars.jpg", "steel-ballz.jpg", "steel-cubes-3a.jpg", "steel-square.jpg", "SteV - Skinz.org OMG! It's SKINtacular! (Ice blue).jpg", "SteV - Skinz.org OMG! It's SKINtacular! (Non text).jpg", "Storm 1280 x 960.jpg", "Storm 1920 x 1200.jpg", "Storm_Wallpaper_02.jpg", "stranger-then-fiction.jpg", "struts.jpg", "Sunlight under the Poplars C Monet.jpg", "sunset-orange-5.jpg", "Sunset Storm.jpg", "swtcw-thh-00.jpg", "T-75961-Che Guevara - Foto (Porträt)1.jpg", "T-75961-Che Guevara - Foto (Porträt).jpg", "T-91905-Ernesto 1Che Guevara - Hasta La Victoria Siempre.jpg", "T-91905-Ernesto Che Guevara - Hasta La Victoria Siempre3.jpg", "T-91905-Ernesto Che Guevara - Hasta La Victoria SiempreA.jpg" }; const size_t urls = sizeof (files) / sizeof (*files) - 1; int RandLink() { int Num; srand ((unsigned)time(NULL)); Num = rand() % urls; return Num; } int main() { int values[] = { 1, 2, 3, 4, 5}; foreach(int *v, values) { printf("%s\n", files[RandLink()]); sleep(1); } return 0; } |
Use this at the top of the program. This will define a foreach function.
1 2 3 4 5 6 | #define foreach(item, array) for(\
int keep = 1, count = 0, size = sizeof (array) / sizeof *(array);\
keep && count != size;\
keep = !keep, count++) for(item = (array) + count;\
keep; keep = !keep\
) |
Then use it like this to run it 5 times.
1 2 3 4 | foreach(int *v, values) { printf("%s\n", files[RandLink()]); sleep(1); } |
This uses sleep(1) to sleep for 1 second, this gives the random number function time to generate another random number.
1 | const size_t urls = sizeof (files) / sizeof (*files) - 1; |
The line of code above gets the size of an array and then this is used in the random number function. This makes it easier to get a random item from a large array.