This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Closed 9 years ago.
I have a function generating random numbers. Why does it always generate the same ones? I tried running the algorithm several times but always get the same results.
#ifndef UTIL_H
#define UTIL_H
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#define MIN 0
#define MAX 100000
void randomArray (double *array, int length)
{
int i ;
for (i = 0; i < length; i++)
{
array[i] = (double) (rand () /
(((double) RAND_MAX + 1) / (double) (MAX - MIN + 1))) + MIN;
}
}
int main(void)
{
int i;
double test_array[9];
randomArray(test_array, 9);
for(i = 0; i < 9; i++)
printf("%f ", test_array[i]);
printf("\n");
return 0;
}
You need to seed the rand function. Use srand(time(NULL)) in the beginning of your main.
There are 3 problems in your code:
1) Add srand to your main() function:
int main(void) {
int i;
double test_array[9];
srand (time(NULL)); // here it is
randomArray(test_array, 9);
for(i = 0; i < 9; i++)
printf("%f ", test_array[i]);
printf("\n");
return 0;
}
2) Add an stdio.h library for using printf():
#include <stdio.h>
3) There is unterminated #ifndef that will couse an error when compiling.
Add #endif:
#ifndef UTIL_H
#define UTIL_H
#endif // here it is
Related
I wrote this program, which is supposed to print all combinations whose sum is equal to a given number.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "combinations.h"
int c = 0;
void findsubsets(unsigned int *numnumbers, int n, int i) {
int j;
if (i < 0)
return;
int sum = 0;
for (j = 0; j < n; j++) {
if (i & (1 << j)) {
sum = sum + numnumbers[j];
}
}
if (sum == target) {
c++;
for (j = 0; j < n; j++) {
if (i & (1 << j))
printf("%u ", numnumbers[j]);
}
printf("\n");
}
}
int main() {
unsigned int iterations = pow(2, VECTOR_SIZE);
for (int i = 0; i < iterations - 1 ; i++)
findsubsets(numbers, VECTOR_SIZE, i);
return 0;
}
//header file next
#ifndef Header_h
#define Header_h
#define VECTOR_SIZE 6
unsigned int numbers[VECTOR_SIZE] = {1, 2, 4, 3, 2, 1};
unsigned int target = 4;
#endif /* Header_h */
However, if as in the example in the header, a number is repeated in the given vector, then it prints out some combinations that include the same numbers. Is there a way to prevent it from doing so? I'm thinking maybe I should use linked lists (which would include all the valid combinations and then compare them, eliminate the ones that are equal, and print out the rest) but I'm pretty sure that would be very time-consuming to write and maybe not very efficient. Is there a less complicated way to do this?
Thank you
I am working on generate 100 random numbers and place them in a , numbers should be 0-999. I wrote my program and it didn't print the random numbers.
I appreciate any help.
this is my code
#include <stdio.h>
#defin S 100
int main()
{
int x;
int a [S];
a[S]=100;
for(x=0;x<s;x++){
printf(a[x]);
}
return 0;
}
Two things: first, int a [S]; a[S]=100 exceeds the array bounds (max is S-1).
Second, printf(const char* format, ...) expects a format string, but you pass an integer value at the place of the format string (turn on compiler warnings!). So write printf("%d ", a[x]), and the program should at least print out some numbers (once you actually assign any numbers to a).
like this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define SIZE 100
#define RANGE 1000
int main(void){
srand(time(NULL));
int a[SIZE];
bool chosen[RANGE] = {0};
for(int i = 0; i < SIZE; ++i){
int select = rand() % RANGE;//select 0..RANGE-1
while(chosen[select]){//check duplicate
if(++select == RANGE)
select = 0;//reset
}
chosen[select] = true;//selected
a[i] = select;
}
//result
for(int i = 0; i < SIZE; ++i)
printf("%d ", a[i]);
puts("");
return 0;
}
I need in c code that generates two numbers in horizontally...so that i can get token numbers for my login system.
I need that i get this:
token=0.152644,0.429187
so in example i have token= and random generated numbers that have at beginning 0. and then 6 random generated numbers separated with , sign.
How to get get this in C?
I have try this code but it does not give me what i want_
#include <stdio.h>
#include <string.h>
typedef
union
{
char tmp[sizeof(unsigned long long)];
unsigned long long myll;
} ll_t;
unsigned long long llrand(void)
{
FILE *in=fopen("/dev/urandom", "r");
ll_t ll_u;
fread(ll_u.tmp, sizeof(ll_u.tmp), 1, in);
fclose(in);
return ll_u.myll;
}
int main()
{
char tmp1[64]={0x0};
char working[64]={0x0};
int i=0;
for(i=0; i< 1; i++)
{
while(strlen(tmp1) < 6)
{
sprintf(working, "%lu", llrand() );
strcat(tmp1, working);
}
tmp1[6]=0x0;
printf("%s\n", tmp1);
*tmp1=0x0;
}
return 0;
}
From output i get this:
747563
102595
Can code be simple and short?
You can use rand() function:
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
int randomNumber(int min, int max)
{
/* generate secret number between min and max: */
int res = rand() % (max-min+1) + min;
return res;
}
int main()
{
int i = 0;
srand (time(NULL));
for (i=0; i<100; i++)
printf("%d ", randomNumber(10, 1000000));
return 0;
}
That is full detail for rand():
http://www.cplusplus.com/reference/cstdlib/rand/
Here is the code that is working perfect:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n1, n2;
time_t t;
srand((unsigned) time(&t));
n1 = rand() % 1000000 + 1;
n2 = rand() % 1000000 + 1;
printf("token=0.%d,0.%d\n", n1, n2);
return 0;
}
And output is:
token=0.289384,0.930887
A propose a different approach. Instead of generating 2 numbers and format them into the output string, generate 12 different digits and put them directly in place.
srand(time(0));
char output[] = "taken=0.XXXXXX,0.YYYYYY";
for (int n = 0; n < 2; n++) {
for (int k = 0; k < 6; k++) {
output[9 * n + 8 + k] = rand() % 10 + '0';
// you might want to write a function that deals with rand() bias
}
}
puts(output);
Total noob here. Can someone give me an example on how i can generate a 2kHz sine wave array with white noise of variance 0.01 in C? This is what I have so far:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.141592653589793
int main() {
int i;
double buffer[10000];
for(i = 0; i < 10000; i++)
{
buffer[i] = sin(2000 * (2 * PI) * i / 6000) + sqrt(0.01)rand;
}
return 0;
}
(For reference)
You first have to seed the random-number generator using srand(), to which you should pass a unique value at every program-run.
Your code, modified for correctness:
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand((unsigned)time(NULL));
int i;
double buffer[10000];
for(i = 0; i < 10000; i++)
{
buffer[i] = sin(2000 * (2 * M_PI) * i / 6000) + sqrt(0.01) * rand();
}
/* ... */
return 0;
}
This question already has answers here:
srand() — why call it only once?
(7 answers)
Closed 7 years ago.
I want to fill a table with random numbers and calculate the time it takes to fill it.
Times seems ok but after I print the table the numbers aren't random.
It prints: 0 1 2 3 4 5 6 7 8 9
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
int getUniqueNumber(int *p, int i);
int main()
{
int i, p[N];
long t0, t1, dt;
printf("Getting %i random numbers...",N);
time(&t0);
for (i=0; i<N; i++)
p[i] = getUniqueNumber(p,i);
time(&t1);
printf("Task Completed.\n\n");
dt = t1 - t0;
printf("Calc time = %ld\n",dt);
for(i=0; i<N; i++)
printf("%d ",i);
return 0;
}
int getUniqueNumber(int *p, int i)
{
int x, j, found;
do
{
srand(time(NULL));
x = rand();
found = 0;
j = 0;
while (j<=i && found == 0)
{
if (p[j] == x)
found = 1;
else
j++;
}
}while (found == 1);
return x;
}
Note: N in #define will be 30000.
I made it 10 for ease.
You should call srand(time(NULL)); only once.