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 |
What are bitwise shift operators in c programming?
If we have an array of Interger values, find out a sub array which has a maximum value of the array and start and end positions of the array..The sub array must be contiguious. Take the start add to be 4000. For Ex if we have an array arr[] = {-1,-2,-5,9,4,3,-6,8,7,6,5,-3} here the sub array of max would be {8,7,6,5} coz the sum of max contiguous array is 8+7+6+5 = 26.The start and end position is 4014(8) and 4020(5).
5 Answers Microsoft, Motorola,
void main() { int x=25,y=32; clrscr(); x=x++ + y++; y=++x + ++y; printf("%d%d",x,y); }
why ordinary variable store the later value not the initial
What is the value of uninitialized variable in c?
How can I read a directory in a C program?
2 Answers Bright Outdoor, Wipro,
Explain how do I determine whether a character is numeric, alphabetic, and so on?
Which of the following about automatic variables within a function is correct ? a.its type must be declared before using the variable b.they are local c.they are not initialised to zero d.they are global.
Explain what is the advantage of a random access file?
Write a program that his output 1 12 123
What is the difference between null pointer and void pointer
10 Answers CTS, Manforce, MAQ Software,
What is the size of array float a(10)?