How to randomize numbers with no repeat in C? [duplicate] - c

This question already has answers here:
Unique (non-repeating) random numbers in O(1)?
(22 answers)
Unique random number generation in an integer array [duplicate]
(9 answers)
Closed 8 years ago.
I want to randomize number in each element of array in a variabel. I currently use srand() function. But, with this function i could get a same number in two or more element of array.
the output of my program is
number[0]=6
number[1]=3
number[2]=8
number[3]=3
See, number[1] and number[3] has same value. How to prevent this thing happen?

Related

How to create random number values in c [duplicate]

This question already has answers here:
Recommended way to initialize srand?
(15 answers)
srand() — why call it only once?
(7 answers)
Closed 2 years ago.
i am trying to create random data so i can review an algorithm in cache policy. When i use the first code with rand() i get the same numbers every time i run my code(i knew this was coming) so i tried to use srand. when i inserted it i get different numbers every time i run the code but the same 10 numbers at my loop. What am i doing wrong to create random data??? After that i will make with zip ununiformed data.
sorry my bad the loop is in main function:
for(counter=0; counter<10; counter++){
printf("Number %d is %lf\n", counter, rand_val());
}
And i am using this function to create numbers.
double rand_val(){
double rnum;
srand(time(NULL));
rnum=rand();
return(rnum);
}

How to make an intermediate printf()? [duplicate]

This question already has answers here:
How to pass variable number of arguments to printf/sprintf
(7 answers)
C: Passing variable number of arguments from one function to another
(5 answers)
Closed 2 years ago.
Suppose I have a function name my_print(int flag_1, /* what goes here? */).
Its objective is the same as printf(), but it should only print when flag_1 is "true".
How would I write the function's header and body?
Note: I know I can just write if (flag_1) printf(/*something*/);, but how would I put that into a function?

finding the size of an unknown array in C [duplicate]

This question already has answers here:
How do I determine the size of my array in C?
(24 answers)
Closed 4 years ago.
We have been given a large array of unknown size with elements given , is there any function or something other through which we can find the size of that array in language C
int a[]={4,6,4,26,3,2,5,7,3,7,3,2,5,4,6,3,7,232,6,32,6,3,7,3,6,2,5,7,3,6,3,6,36,3,67,23,6};
The size in bytes you can get by
sizeof(a);
The number of elements in that array you can get by
sizeof(a)/sizeof(a[0]);

Move array in array of arrays [duplicate]

This question already has answers here:
How to change the position of an array element?
(8 answers)
Closed 6 years ago.
I have an array of arrays like this:
[['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l']]
Now I want to move the last array ['j','k','l'] after array ['a','b','c'] and before array ['d','e','f']. How can I do this?
array.insert(1, array.delete_at(3))
This should do it.

force length of int from a sensor [duplicate]

This question already has answers here:
Printing leading 0's in C
(11 answers)
Closed 6 years ago.
I get data from a sensor in int type but the data range from 0 to 999.
I need a simple way for the var to always be the same length.
Like if I get 123 I don't need to change it but if it's 43 I need it to be 043.
Is there a simple way to do that?
Thanks.
Try with:
printf("%03d", var);

Resources