Solved

Random number??

Posted on 1998-02-14
18
301 Views
Last Modified: 2010-04-10
How do I have Builder (v1.0 for win 95) generate a random number (10 digits long) then have it save it in regnum.dat??
 
thanks
0
Comment
Question by:4099aol
  • 8
  • 5
  • 4
  • +1
18 Comments
 
LVL 3

Accepted Solution

by:
q2guo earned 10 total points
ID: 1182069
#include <fstream.h>
#include <stdlib.h>

main ()
{
   unsigned int i, k;
   unsigned long random_num;
 
   ostream fout("regnum.dat");
   
   randomize();
   i = rand();
   k = rand();

   random_num = i * k;
   fout << random_num << endl;
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1182070
That random number won't be 10 digits long.

To get 10 digits I would use an alcoritm that gets a number that is 1 digit long and repeat it 10 times.  Like

char RndDigStr[10];

randomize();
for (int i = 0; i < 10; ++i)
   RndDigStr[i] = '0' + (rand() % 10);

this puts 10 random digits in RndDigStr.  In this case the digits are ASCII digit characters from '0' to '9'.  
0
 

Author Comment

by:4099aol
ID: 1182071
nietod...

You said that the number will be put in the string RndDigStr, well how do I put the numbers in to the dat file?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1182072
In my previous example you could make RndDigStr a NUL terminated sting by declaring it length 11 and adding a NUL terminator with

RndDigStr[10] = 0;

then you could output it with

fout << RndDigStr;

or you could forget RndDigStr and output each digit, like

for (int i = 0; i < 10; ++i)
   fout << (char) '0' + (rand() % 10);


0
 

Author Comment

by:4099aol
ID: 1182073
so do this??

{                          
ostream fout("regnum.dat");
                             
char RndDigStr[10];

randomize();
for (int i = 0; i < 10; ++i)
RndDigStr[i] = '0' + (rand() % 10);
fout << RndDigStr;
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1182074
Not quite.

fout << RndDigStr

will work only if RndDigStr is NUL terminated.  So you would need

{
   ostream fout("regnum.dat");
                               
   char RndDigStr[11]; // Extra room for NUL.

   randomize();
   for (int i = 0; i < 10; ++i)
      RndDigStr[i] = '0' + (rand() % 10);

   RndDigStr[10] = 0; // Terminate.
   fout << RndDigStr;
}

however, if you don't need to save a copy of the digits, drop RndDigStr and just use.

{
   ostream fout("regnum.dat");
                               
   randomize();
   for (int i = 0; i < 10; ++i)
   {
      char chr = '0' + (rand() % 10);

      fout << chr;
   }
}

0
 

Author Comment

by:4099aol
ID: 1182075
Error.  (on both sets of codes)

[C++ Error] webmaster.cpp(81): Could not find a match for 'ostream::ostream(char *)'.
[C++ Warning] webmaster.cpp(86): Conversion may lose significant digits.


0
 
LVL 3

Expert Comment

by:q2guo
ID: 1182076
AnsiString S;
S.SetLength(11);

randomize();

for (int i = 0; i < 10; ++i)
{
   S[i] = '0' + (rand() % 10);
}
S[10] = '\0';

TStringList *MyStringList = new TStringList;
MyStringList->Add(S);
MyStringList->SaveToFile("regnum.dat");
0
 

Author Comment

by:4099aol
ID: 1182077
I am now getting this popup dialog box...

Project webmaste1.exe rasied exception class EOutOfMemory with message 'Out of Memory'.

I restarted my computer (thinking my computer was just running low on RAM, but had the same message.  I took the codes (that u just gave me) out and the app works fine.  Please tell me how do I get it to manage it's memory a little bit better, or fix the codes to sue less memory.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 1182078
Sorry I don't use streams so I wasn't aware that you can't print a "const char *" with a stream.  q2quo's string class solution should work.  Although I've never heard of "AnsiString", the strig supplied by STL that I'm aware of is just called "string".  But I'm not too familiare with STL.

as for th warning about loosing digits, ignore it or use the "(char)" conversion operator.  That is change

char chr = '0' + (rand() % 10);

to

char chr = (char) '0' + (rand() % 10);


0
 
LVL 3

Expert Comment

by:q2guo
ID: 1182079
Add
delete MyStringList;
to the end of my code
so it should read

.
.
.

TStringList *MyStringList = new TStringList;
MyStringList->Add(S);
MyStringList->SaveToFile("regnum.dat");
delete MyStringList;
0
 

Author Comment

by:4099aol
ID: 1182080
nope still out of memory
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1182081
Are your running above code inside a loop?
0
 

Author Comment

by:4099aol
ID: 1182082
I do not think so, it is in FormCreate (Borland C++ Builder)
0
 

Expert Comment

by:larockd
ID: 1182083
Is randomize() a function specific to Borland?  I think I remember using it in Pascal..

Shouldn't he use srand instead to seed the random number generator?  Or does randomize in borland take care of it.  I checked on this in Visual C++ and the closest thing to randomize is randomize() when using vbscript?
0
 

Author Comment

by:4099aol
ID: 1182084
i have no idea.
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1182085
Try this 4099Aol

#include <vcl/classes.hpp>
#include <stdlib.h>

char S[11];
randomize();

for (int i = 0; i < 10; ++i)
{
    S[i] = '0' + (rand() % 10);
}
S[10] = '\0';

AnsiString randstring(S);

TStringList *MyStringList = new TStringList;
MyStringList->Add(S);
MyStringList->SaveToFile("regnum.dat");
0
 

Author Comment

by:4099aol
ID: 1182086
thank you it works!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
SendMessage fails while PostMessage works 2 107
ASP.net build a IF/Then Walkthrough Guide 1 191
is twain_32.dll cmpatible with windows 10 ? 10 128
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now