Basic beginner here. I’m trying to figure out how to get a random number from the following set:
“1, 4, 7, 10, 13, 16”
My knowledge on the rand() is that it can’t take any parameters. Generating a random number from a continuous range is no problem. Get the range then rand() % range - min. However, I’m really racking my brain to figure out how to get a random number from a non-continuous set like the one above.
Up to this point, the book I’m using has not covered arrays or any type of loop. Very basic. I see there is a sequence of adding 3 to itself in that set. Without looping or using an array (ive seen many examples using an array and loops), how can I pull a random number from a similar set?
If you are trying to get a random number from the set of numbers represented. Assume that each number is associated by a natural number. rand() will produce any number in the set of natural numbers. When you use modulus you can make its co domain the size you want it. The natural numbers that you get are then bijective with the set of numbers you want.
Why do you use pointer arithmetic and dereferencing where it is not necessary? IMO it makes the code harder to read, especially for beginners.
Just use the indexing/subscript operator.
Doesn’t this:
values[5] = 16;
Looks better, more pleasant to the eye than this:
(values+5)* = 16;
Also while I am still here. Why use dynamic memory in the example? Usually the Look Up Tables are static anyways.
Also don’t forget to set the seed for random, or it will always choose the same values for every program run. (Good seed is time for example, unless ofc you need it for crypto, but don’t use rand() anyways in that case)
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define SARR_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
int main(void)
{
srand(time(NULL));
int values[] = {1, 4, 7, 10, 13, 16};
int index = rand() % SARR_SIZE(values);
printf("%i\n", values[index]);
return 0;
}
I have not learned about arrays. I have however, learned about pointers and dereferencing. I’m still working on it (i dont have a lot of time to work on it but I use what spare time I have to do it) so I’ll post an update sometime today.
@chiefshane I can totally understand the need to use pointers and dereferencing to get used to them in practice. That said, I looked up the malloc() function and I have no idea really how that works. It’s said to allocate requested memory and return a pointer to it. I noticed you used the sizeof() function which the book touched on in chapter 3 but they didn’t do anything with it other than use it as an example of a unary operator. I imagine further use of pointers and dereferencing are coming along with more functions. In time!
Yes! Nice reminder.
@Sandalmoth
This was the way I decided to go with it. I believe it was the books intention to do a function within a function (thats what im calling it lol). I noticed the 3x+1 but it slipped my mind that I can put in rand() % 6 in as x and set up the parenthesis properly. Makes sense. Thanks for reminding me of this very important fact.
Again, thank you all for you help and suggestions.