Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to pick random index from an array with C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
	int arr[] = { 4, 9, 12, 90, 13, 0, 3, 97 };

	int size = sizeof(arr) / sizeof(arr[0]);

	srand(time(NULL));

	printf("%d", rand() % (size - 1)); // random from 0 to size - 1

	return 0;
}



/*
run:
 
6
 
*/

 



answered Nov 22, 2022 by avibootz
edited Nov 22, 2022 by avibootz

Related questions

1 answer 116 views
2 answers 91 views
1 answer 129 views
1 answer 119 views
1 answer 96 views
...