Rand function won't work as supposed in C [duplicate] - c

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.

Related

change an integer to base 2 with arrays as output in C

#include <stdio.h>
#include<math.h>
int main(void){
int Num;
int i;
int N;
int x = 0;
int a[x];
scanf("%d", &Num);
N = Num;
for(i = 0; i < Num; i++)
{if (Num%2 == 0)
{a[i] = 0;}
else
{a[i] = 1;}
Num = Num/2;
}printf("%d in base 2 is %d", N, a[x]);
return 0;
}
program should convert an integer Num to base 2 eg 17 to 10001.
ideally using an array as the output
the remainder of the division of Num by 2 should be the last number in the output
then number is divided by 2 and the process repeats with the second output becoming the 2nd last output of the array
Sorry if this question is worded badly
Any help is appreciated
Thanks
#include <stdio.h>
#include <math.h>
int main(void){
int Num;
int N;
scanf("%d", &Num);
int x = floor(log2(Num)) +1;
int a[x];
N = Num;
for(size_t i=0;i<x;i++){
if(Num%2 == 0){
a[i]=0;
}else{
a[i]=1;
}
Num /= 2;
}
printf("%d in base 2 is ",N);
for(int i=x-1; i>=0;i--){
printf("%d",a[i]);
}
return 0;
}
I think the above code works fine for what you need (although it's not the best way to code this program).
In line 7 of your code, you defined x as 0; so, in line 8, the length of your array is 0 – and that's not what we need.
At the end, when you want to output the result, you only output the xth element of your array. Instead, we want to output every element that we stored. (I used i-- because, if I did not, the binary of Num would be reversed.)

Creating a function thats generates a random number using srand and rand [duplicate]

This question already has answers here:
srand() — why call it only once?
(7 answers)
Closed 1 year ago.
Trying to create a function that generates a random number using rand() and srand().
This code prints 3 random numbers successfully:
//prints a random number from 1 to 10 3 times.
void main()
{
int num;
srand(time(NULL));
num = rand() % 10;
printf("%d\n", num);
num = rand() % 10;
printf("%d\n", num);
num = rand() % 10;
printf("%d\n", num);
}
//Output: 3 5 6
This code using a function doesn't:
//calls PrintRandomNumber 3 times.
void main()
{
int i=0, num;
for(i=0; i<3; i++)
{
printRandomNumber(0, 10);
}
}
//Prints a random numberin the range of lowestValue to highestValue.
void printRandomNumber(int lowestValue, int highestValue)
{
int randomNumber = 0;
srand(time(NULL));
randomNumber = (rand() % (highestValue - lowestValue + 1)) + lowestValue;
printf("%d\n", randomNumber);
}
//Output: 3 3 3
The problem is calling srand(time(NULL)) multiple times.
Can a function print/return a random number using rand() and srand() without having to call srand(time(NULL)) in the main?
You can to do that as in the example below:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main () {
int i, n;
time_t t;
n = 3;
/* Intializes random number generator */
srand((unsigned) time(&t));
for( i = 0 ; i < n ; i++ ) {
printf("%d\n", rand() % 10);
}
return(0);
}

What is the bug in this in c qsort function

I am trying to run same calculate function with c++ and it works as expected but not with c, could you please tell what is wrong. I am just sorting two arrays comparing value and incrementing answer;
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
int compare_function(const void *a,const void *b) {
int *x = (int *) a;
int *y = (int *) b;
return *x - *y;
}
int calculate(long long A[], long long G[],int t){
int answer = 0;
int j = 0;
int i = 0;
while (i < t) {
if(G[i] > A[j]){
i++;
j++;
answer++;
} else {
i++;
}
}
return answer;
}
int main()
{
int p;
scanf("%d", &p);
while (p > 0){
int t;
scanf("%d", &t);
long long G[t];
long long A[t];
for(int i=0;i<t;i++)
scanf("%d",&G[i]);
for(int i=0;i<t;i++)
scanf("%d",&A[i]);
qsort(G,t,sizeof (long long ),compare_function);
qsort(A,t,sizeof (long long ),compare_function);
printf("%d\n", calculate(A,G,t));
p--;
}
}
I have used c(gcc 8.2.0) and one weird thing is every time I ran this program I got differ output.
Array Input
1
10
3 6 7 5 3 5 6 2 9 1
2 7 0 9 3 6 0 6 2 6
output: 6, expected : 7
constraints
1<= N <=100000
0<= Array elemts <= LLONG_MAX

c program finding call function

Two integers are stored in the arrays a 1 and a 2, respectively, and the product is calculated by the same procedure as the calculation, but it does not output the correct result.
question is : want to produce 312*321 = 1 0 0 1 5 2 but this first program produce
? 0 9 9 11 5 2
to produce right result 1 0 0 1 5 2, call function name func(c,N*2)
#include <stdio.h>
#include <stdlib.h>
#define N 3
int main()
{
int a1[N]={1,2,3};
int a2[N]={2,1,3};
int b[N][N];
int c[N*2];
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++)
b[i][j]=a1[j]*a2[i];
}
c[0]=b[0][0];
c[1]=b[0][1]+b[1][0];
c[2]=b[0][2]+b[1][1]+b[2][0];
c[3]=b[1][2]+b[2][1];
c[4]=b[2][2];
for(i=N*2-1;i>=0;i--)
{
printf("%d ",c[i]);
}
printf("\n");
return 0;
}
The result:0 9 9 11 5 2
  |0|1|2| ->A1
----------------
A2<-| 0|2|4|6|
| 1|1|2|3|
| 2|3|6|9|
this array is the same as 321*312 calculate using hand in paper
Problem: Define the function func () to output the correct result 1 0 0 1 5 2, call func (c, N * 2); below i post the code with the call function func() in bold. any idea?? and also what the logic behind func()? trial and error? is there algorithm behind this?
#include <stdio.h>
#include <stdlib.h>
#define N 3
int main()
{
int a1[N]={1,2,3};
int a2[N]={2,1,3};
int b[N][N];
int c[N*2];
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++)
b[i][j]=a1[j]*a2[i];
}
c[0]=b[0][0];
c[1]=b[0][1]+b[1][0];
c[2]=b[0][2]+b[1][1]+b[2][0];
c[3]=b[1][2]+b[2][1];
c[4]=b[2][2];
**func(c,N*2);**
for(i=N*2-1;i>=0;i--)
{
printf("%d ",c[i]);
}
printf("\n");
return 0;
}
**void func(int a[],int digit)
{
here no idea....
}**
Try with this;
void func(int a[], int digit)
{
int i, c = 0;
for(i = 0; i < digit; i ++)
{
a[i] += c;
c = a[i] / 10;
a[i] = a[i] % 10;
}
}
I think you should change two things:
First initialization the result array
int c[N * 2] = {0}; //initialize
Also the function looks like
void func(int a[], int size) {
int carry = 0;
for (int i = 0; i < size; i++) {
a[i] += carry;
carry = a[i] / 10;
a[i] = a[i] % 10;
}
}

Generate different numbers in C using Random [duplicate]

This question already has answers here:
Generating random numbers in C
(7 answers)
Closed 9 years ago.
So basically i wrote this function in C to generate 5 random numbers from 1 to 50:
#include <stdio.h>
#include <stdlib.h>
int main() {
int c, n;
printf("Five random numbers from 1 to 50 \n");
for (c = 1; c <= 5; c++) {
n = rand()%50 + 1;
printf("%d\n", n);
}
return 0;
}
and i'd like to know how can i be sure that the numbers generated by this code are all different from each other.
Any help ?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int i, c, n, size = 50;
int data[size];
srand(time(NULL));
for(i=0;i<size;++i)
data[i] = i+1;
printf("Five random numbers from 1 to 50 \n");
for (c = 1; c <= 5; c++) {
n = rand()%size;
printf("%d\n", data[n]);
swap(&data[--size], &data[n]);
}
return 0;
}
Store each number in an array and check each new random number against the existing entries.
You should check all numbers like this:
#include <time.h>
int randomNumbers[5];
int c, d, n;
bool itIsNew;
//current time as random seed
srand(time(0));
for (c = 1; c <= 5;)
{
n = rand() % 50 + 1;
itIsNew = true;
for(d = 1; d <= c; d++)
{
if(n == randomNumbers[d - 1])
{
itIsNew = false;
break;
}
}
if(itIsNew)
{
randomNumbers[c - 1] = n;
c++;
}
}

Resources