| | |
Generating random numbers using VC6
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2007
Posts: 30
Reputation:
Solved Threads: 0
Hi , im trying to create a simple code that generate random numbers
Im having two problem ,
1. For some reason randomize() function does not work on VisualC++6
2. I didnt really got the idea on how to set the range of the numbers that will be generated using the rand() function ,. i just saw it in some example .
Here is the code:
Im having two problem ,
1. For some reason randomize() function does not work on VisualC++6
2. I didnt really got the idea on how to set the range of the numbers that will be generated using the rand() function ,. i just saw it in some example .
Here is the code:
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main() { int i; randomize(); for (i=0;i<10;i++) printf ("Number %d: %d\n",i+1,rand()%10+1); return 0; }
Last edited by RisTar; Apr 12th, 2007 at 8:24 am.
hey,
i will like you to try this for C, i use VC6 but the program is C++ and i get my random numbers anytime i want....
first include this
after that.. whenever you want to generate the random numbers,
wil generate one of 0, 1, 2, 3;
now if you want the number to be in the range 1 to 4, just use the code
now you will get number in the range 1,2,3,4;
note 1 is the smallest and 4 is the largest;
if you want to generate the numbers 2, 4, 6, 8...
its by doing the same but adding 2 instead
i will like you to try this for C, i use VC6 but the program is C++ and i get my random numbers anytime i want....
first include this
C++ Syntax (Toggle Plain Text)
#include <cstdlib> using std::rand;
after that.. whenever you want to generate the random numbers,
C++ Syntax (Toggle Plain Text)
rand()%4
wil generate one of 0, 1, 2, 3;
now if you want the number to be in the range 1 to 4, just use the code
C++ Syntax (Toggle Plain Text)
1 + rand()%4;
now you will get number in the range 1,2,3,4;
note 1 is the smallest and 4 is the largest;
if you want to generate the numbers 2, 4, 6, 8...
its by doing the same but adding 2 instead
C++ Syntax (Toggle Plain Text)
2 + rand()%4;
Last edited by addicted; Apr 12th, 2007 at 11:46 am. Reason: the code was for c++
00110101
![]() |
Similar Threads
- Compile time errors in C++ while generating random numbers (C++)
- C++ Random Numbers (C++)
- Logic behind generating random number (C)
- generating random numbers into an array.. (Java)
- problem in generating non repeated random numbers (C)
Other Threads in the C++ Forum
- Previous Thread: Borland C++
- Next Thread: Implementation file
Views: 1784 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






