-4

I am trying to generate a random in the range of the array size, and print the random element from the array then return the index of such element. This is what I have so far. The code isn't working. Can anybody please help me?

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

srand ( time(NULL) );
int myArray[11] = { 4,8,2,5,9,1,7,10,43,23,3 };
int randomIndex = rand();
int randomValue = myArray[randomIndex];
printf("Reference: %d\n", randomValue);
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
SarahB
  • 1
  • 1
  • 1
  • 3
  • 2
    What exactly is the problem? Are you getting an error? An unexpected result? – Mureinik Oct 10 '17 at 20:58
  • this is the error: syntax error near unexpected token `time' – SarahB Oct 10 '17 at 20:59
  • 2
    `rand() % 11` ? – Stargateur Oct 10 '17 at 20:59
  • 2
    Do you have a `main` function? – Mureinik Oct 10 '17 at 21:00
  • you're going to want to change that to `int randomIndex = rand() % 11;`. `rand()` returns a value between 0 and `RAND_MAX`, which is most certainly going to be larger than 11. `myArray` indexed at anything greater than 10 is undefined behavior. – yano Oct 10 '17 at 21:00
  • @SarahB the code won't compile as you lack `;` in the line that generates the index. What's more you updated the question to include what appears to be the answer. I would suggest accepting one of the answers (if they meet your requirements) and move on. – orhtej2 Oct 10 '17 at 21:12
  • @orhtej2 well it still doesn't compile..thank you for catching that! – SarahB Oct 10 '17 at 21:14
  • 1
    Rolled back. You must not change the question once you have answers leaving them without context! A good C book would be a helpful reading. – too honest for this site Oct 10 '17 at 21:15
  • 1
    It is extremely annoying when questions are edited in such a manner as to invalidate existing answers. It's essentially vandalism:( – Martin James Oct 10 '17 at 21:16
  • 1
    What specifically is unclear about the last two comments? https://meta.stackoverflow.com/a/298799/4774918 – too honest for this site Oct 10 '17 at 21:26
  • well it still doesn't run..and I apologize about that..I am not that familiar with SO..won't do that in the future. – SarahB Oct 10 '17 at 21:31
  • yes, i have a main function @Mureinik – SarahB Oct 10 '17 at 21:32
  • 2
    'well it still doesn't run' is not useful. Look, make a MCVE by enclosing the code in a valid main() function or by calling a fuction enclosing the above code from main()., fix the out-of-bounds addressing, compile, link, test what you then have and, if there is still an issue, please ask another question. – Martin James Oct 10 '17 at 21:33
  • How about getting a [good C book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) - get one for modern C, i.e. C99 at leat, best standard C11) and learn the language properly from the beginning. Requiring at least `int main(void)` (`int main()` is a legacy and obsolescent) is shown with the very first program you learn. Obscure youtube videos, blogs or online tutorials are not appropriate. And we are not a tutoring site. – too honest for this site Oct 10 '17 at 21:34
  • @Olaf It had that in there. Thanks for the input. – SarahB Oct 10 '17 at 21:36

2 Answers2

1

First, it seems that your code is outside a function, so wrap it by, for example, function main.

Second, function rand returns a pseudo-random integer value between ​0​ and RAND_MAX, which is usually a value much higher than your array size 11. Hence, you have a very good chance to exceed array bounds.

Limit the randomIndex to 11, e.g. by using modulo:

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

int main() {
    srand ( time(NULL) );
    int myArray[11] = { 4,8,2,5,9,1,7,10,43,23,3 };
    int randomIndex = rand() % 11;
    int randomValue = myArray[randomIndex];
    printf("Reference: %d\n", randomValue);
    return 0;
}

Note that modulo (i.e. %11) is a very simple method to cap the number, and it yields a non-uniform distribution of random values. If this gets a problem, please do not hesitate to ask again.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
-1

rand() generates a value between 0 and RAND_MAX, you should cap it to array length i.e. like here

const int N = 11; // length of myArray
int randomIndex = rand() / (RAND_MAX / N + 1);
orhtej2
  • 2,133
  • 3
  • 16
  • 26
  • #include #include #include int main() { srand ( time(NULL) ); int myArray[11] = { 4,8,2,5,9,1,7,10,43,23,3 }; const int N = 11; // lenght of myArray int randomIndex = rand() / (RAND_MAX / N + 1) printf("Reference: %d\n", randomValue); return 0; } – SarahB Oct 10 '17 at 21:07
  • @SarahB seems reasonable, does it work as expected? – orhtej2 Oct 10 '17 at 21:10
  • no, it doesn't. I edited the code in the original question. Not sure why its showing this now.. line 4: syntax error near unexpected token `(' line 4: `int main()' – SarahB Oct 10 '17 at 21:12
  • @SarahB Yes, there's a semicolon missing. – orhtej2 Oct 10 '17 at 21:12
  • `rand() / (RAND_MAX / N + 1)` does not distribute `rand()` that evenly in that `randomIndex` comes up `10` less often than `0`-`9` (maybe 10 less in about 200,000,000). `rand() %11` is a flatter distribution. IAC a fair distribution is more work. – chux - Reinstate Monica Oct 10 '17 at 21:16
  • @orhtej2 no there is no semicolon missing..its the main – SarahB Oct 10 '17 at 21:46
  • @chux I fail to see that. That's one bucket skewed (if any) vs `RAND_MAX % 11` buckets favoured. Can you please explain? – orhtej2 Oct 10 '17 at 21:55
  • 2
    @orhtej2 "That's one bucket skewed" is not so with this method. With `rand()%M` (M==11) the eleven buckets, some buckets will have `N` and others will have `N+1` occurrences. With this approach, example `(RAND_MAX + 1 == 2**31)`: The first ten numbers will occur `P` times each and the eleventh will have `P-9`occurrences. More unbalanced. This answer's approach has merit with FP math, yet with `int` math, I see it as weaker than `rand()%11` given the last number (worst case) could occur about M less than the others. – chux - Reinstate Monica Oct 10 '17 at 22:32