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 is difference between constant pointer and constant variable?
what is the other ways to find a logic to print whether a number is an even or odd wit out using % symbol??????? i know three different ways to print it. so i need any other different logic>>>>>
Does c have enums?
write a c program to find largest number in matrix(in each row,each column, diagonally, and in the whole matrix)? Its urgent.
1)what is the error in the following stmt where str is a char array and the stmt is supposed to traverse through the whole character string str? for(i=0;str[i];i++) a)There is no error. b)There shud be no ; after the stmt. c)The cond shud be str[i]!='\0' d)The cond shud be str[i]!=NULL e)i shud be initialized to 1
What are the 5 organizational structures?
what is the difference between embedded c and turbo c ?
#include<stdio.h> #include<conio.h> struct stu { int i; char j; }; union uni { int i; char j; }; void main() { int j,k; clrscr(); struct stu s; j=sizeof(s); printf("%d",j); union uni u; k=sizeof(u); printf("%d",k); getch(); } what is value of j and k.
Write a program to print all the prime numbers with in the given range
8 Answers ABC, College School Exams Tests, TCS,
Explain how do you declare an array that will hold more than 64kb of data?
I have an array of 100 elements. Each element contains some text. i want to: append a star character to the end of every fifth element remove every second character from every tenth element, and… add a line feed (ascii 10) after the 30th character of every array element whose length is greater than 30 characters.
int i=~0; uint j=(uint)i; j++; printf(“%d”,j);