Here's the code for my program. it's supposed to get two numbers from user then generate random numbers using rand function. but when I compile it, the numbers are not random.
I also wrote this in java, and that one works perfectly.
C version:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int cmpfunc();
int main()
{
puts("How many numbers do you want to generate?");
int n;
scanf("%d", &n);
int numbers[n];
puts("What should be the largest number possible?");
int m, i;
scanf("%d", &m);
int result[m];
for(i=0;i<m;i++)
numbers[i] = i + 1;
srand(time(NULL));
for(i=0;i<n;i++)
{
int r = rand() % m;
result[i] = numbers[r];
numbers[r] = numbers[m - 1];
m--;
}
qsort(result, n, sizeof(int), cmpfunc); //sorting the result array
for(i=0;i<n;i++)
{
printf("%d\n", result[i]);
}
getch();
return 0;
}
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
EDIT: srand was inside the loop, people said it's not supposed to be called more than once. so i took it out but that wasn't helpful.
Java version:
import java.util.*;
public class RandomNumberGenerator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("How many numbers do you want to generate?");
int n = in.nextInt();
System.out.println("What should be the largest number possible?");
int m = in.nextInt();
int numbers[] = new int[m];
for(int i=0;i<numbers.length;i++)
numbers[i] = i + 1;
int result[] = new int[n];
for(int i=0;i<result.length;i++)
{
// make a random index between 0 and n - 1
int r = (int) (Math.random() * m );
result[i] = numbers[r];
// move the last element into the random location
numbers[r] = numbers[m - 1];
m--;
}
System.out.println("Do you want the result to be sorted? (1 for Yes, others for No)");
int ans = in.nextInt();
if(ans==1)
Arrays.sort(result);
System.out.println("Result:");
for (int r : result)
System.out.println(r);
pressAnyKeyToContinue();
}
private static void pressAnyKeyToContinue()
{
System.out.println("Press Enter to continue.");
try
{
System.in.read();
}
catch(Exception e)
{}
}
}
edit 2:
c output: (is not random, sorted)
java output: (is random, sorted)
edit 3:
as i stated in comments values are : n= 55 m= 9808
also i'm using codeblocks so header files are not really an issue. they are automatically loaded by codeblocks.
edit 4:
I'm really impressed by what people suggest. qsort doesn't change a thing in generating random numbers. it just sorts the array. so even if i remove it, it doesn't change nothing.
In an array of random numbers we would expect that the difference between any two adjacent elements would also be random. This property is preserved under sorting. I sort the random numbers in both the C and Java could to check this property. As you can see, in the C code the differences between adjacent elements are all the same. This leads me to believe that the C code is not generating random numbers, but that the Java code is.
edit 5:
and i'm sure there should be no difference in algorithm between java and c.
edit 6:
I'm sorry that i'm not a professional. i'm just trying to make a C version of the Java program i have. A fixed code would be very appreciated.
****edit final:** Finally got it working. arrays "numbers" and "result" storing n and m respectively should store m and n instead. that's all. in addition to that i changed their names with what people suggested. some folks told me to switch the variables m and n with array dims. that just wasn't the case. here is the fixed code for anyone interested:**
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int cmpfunc();
int main()
{
puts("How many numbers do you want to generate?");
int num_numbers;
scanf("%d", &num_numbers);
puts("What should be the largest number possible?");
int num_maximum, i;
scanf("%d", & num_maximum);
int numbers[num_maximum];
int result[num_numbers];
for(i=0;i<num_maximum;i++) // making
numbers[i] = i + 1;
srand(time(NULL)); // seeding rand
for(i=0;i<num_numbers;i++)
{
int r = rand() % num_maximum; // generates random number between 0 and m-1
result[i] = numbers[r]; //
numbers[r] = numbers[ num_maximum - 1];
num_maximum--;
}
puts("Do you want the result to be sorted?");
int ans;
scanf("%d", &ans);
if (ans==1)
qsort(result, num_numbers, sizeof(int), cmpfunc); //sorting the result array
for(i=0;i< num_numbers;i++)
{
printf("%d\n", result[i]);
}
getch();
return 0;
}
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}