How to write a code for random pick from 1-1000 numbers?
The output should contain the 10 numbers from the range
1-1000 which should pick randomly, ie ,for each time we run
the code we should get different outputs.
Answers were Sorted based on User's Feedback
Answer / navdeep singh
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int i;
randomize();
printf("Ten random numbers from 0 to 1000\n\n");
for(i=0; i<10; i++)
printf("%d\n", rand() % 100);
return 0;
}
Is This Answer Correct ? | 15 Yes | 7 No |
Answer / kameshwar
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int i;
srand(time(NULL));
printf("\n Ten random numbers between 1 and 1000 are \n");
for(i=0;i<10;i++)
printf("%d ",(rand() % 1000) + 1);
return EXIT_SUCCESS;
}
Is This Answer Correct ? | 5 Yes | 0 No |
Answer / kap
it is not the correct answer because the range they asked 0-
1000 so need to to %1000. :-)
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / nischal e rao
The best way to solve this problem would be to create a hash
function and apply the current time to as the argument to
the hash function. the hash function should be designed to
return a number between 1 to 1000.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / sharan
if the randomize(); doesn't work
replace it with srand ((unsigned) time (NULL));
Is This Answer Correct ? | 2 Yes | 2 No |
Answer / abhradeep chatterjee
I am Sorry, I executed the Code and it does not work.
Thanks to Rama Krishna.
Is This Answer Correct ? | 1 Yes | 2 No |
Answer / rama krishna
First program is wrong ,randomize is undefined you just
check it.
Is This Answer Correct ? | 0 Yes | 1 No |
pgm to find number of words starting with capital letters in a file(additional memory usage not allowed)(if a word starting with capital also next letter in word is capital cann't be counted twice)
What are local and global variables?
What is line in c preprocessor?
Create a structure to specify data on students given below: Roll number, Name, Department, Course, Year of joining Assume that there are not more than 450 students in the college. 1.write a function to print names of all students who joined in a particular year 2.write a function to print the data of a student whose roll number is given
The C language terminator is a.semicolon b.colon c.period d.exclamation mark
how does printf function work
Which is best book for data structures in c?
Write a program to generate random numbers in c?
What is fflush() function?
write an interactive program to generate the divisors of a given integer.
What does 1f stand for?
3. Program to print all possible substrings. ex: String S St Str Stri Strin String t tr tri trin tring r