| | |
Random number generation from an array in C programming
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
Hello friends,
Need help in generating a random number from an array.Once a number is generated, the number has to be deleted from the array so that only the remaining numbers can be generated from the array.And it should go on until all the numbers from the array are deleted.
Hope replies asap from you friends.
Need help in generating a random number from an array.Once a number is generated, the number has to be deleted from the array so that only the remaining numbers can be generated from the array.And it should go on until all the numbers from the array are deleted.
Hope replies asap from you friends.
Last edited by Ancient Dragon; Feb 2nd, 2009 at 10:15 pm. Reason: remove bold tags
>but was unable to delete the number that have already appeared once.
http://www.daniweb.com/forums/thread38345.html
http://www.daniweb.com/forums/thread38345.html
*Voted best profile in the world*
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> int pick() { int arr[5]={2,3,4,5,6}; int random; srand(time(NULL)); random = arr[rand() % 5 ]; return random; } int main() { pick(); int i = 0; while(i<5) { printf("random number from the array %d\n",pick()); } getch(); }
Last edited by Ancient Dragon; Feb 2nd, 2009 at 10:02 pm. Reason: add code tags
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
•
•
•
•
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int pick()
{
int arr[5]={2,3,4,5,6};
int random;
srand(time(NULL));
random = arr[rand() % 5 ];
return random;
}
int main()
{
pick();
int i = 0;
while(i<5)
{
printf("random number from the array %d\n",pick());
}
getch();
}
This is code i have written and stuck up on how to delete the number that have already appeared once
http://www.daniweb.com/code/snippet1034.html
Also, what "erase" command are you referring to in your earlier post. Here's an "erase" command from the vector library.
http://www.cplusplus.com/reference/s...tor/erase.html
You would have to have a vector rather than an array to use it.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: C++ function??
- Next Thread: How to conduct a binary_search inside a function.
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






