# [SOLVED]C programming rand() in a set

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.

int index = rand() % 6;
int* values = malloc(6 * sizeof(int));
values* = 1;
(values+1)* = 4;
(values+2)* = 7;
(values+3)* = 10;
(values+4)* = 13;
(values+5)* = 16;
printf("%i", (values+index)*);
free(values);

So in this case, you have 6 numbers right?

this is roughly all you need to do

int random1 = rand()%6;
int random2 = -1;
switch(random1)
case 0 :
random2 = 1;
break;
case 1 :
random2 = 4;
break;
case 2 :
ran…

You get the point?

Just generate a random set of numbers and project them using preferably a switch case or optionally an if else. Pretty simple.

Another option is to create an array of values, and again rand()%(array_length)

Then take the array[random value] to find the new value. Technically the simplest method you could use.

I have a little gripe with your example code.

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;
}
``````

And running it multiple times:

`````` \$ gcc -Wall -Wextra -Wshadow -pedantic c.c
\$ ./a.out
16
\$ ./a.out
1
\$ ./a.out
13``````
5 Likes

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.

Thanks for your input @oldgek, @chiefshane and @DeltaTech .

For the set in your example there exists a simpler, although less general solution, since it is described by a simple mathematical function

"0, 1, 2, 3, 4, 5"
which you can easily get from rand() can be converted to
“1, 4, 7, 10, 13, 16”
by passing it through the function

f(x) = 3x + 1

2 Likes

I agree your way is better but I wrote it that way because I think its good for beginners to see more pointer examples.

@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.

Summary

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

int main(void)
{
int randNo = 0;

``````srand(time(NULL));

randNo = 3 * (rand() % 6) + 1;
printf("%d", randNo);

return 0;
``````

}

1 Like

technically its not a function, its syntax for the C language.