Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Random character/number generator in MFC?

Posted on 2004-03-22
8
Medium Priority
?
929 Views
Last Modified: 2013-11-20
i'm creating a mfc program... which takes bookings of tickets... so i need to generate a random characters/numbers for the booking id. how do i do that? i try to use

bookingid = rand();

what comes out is

')'
0
Comment
Question by:harsher
  • 2
  • 2
8 Comments
 

Author Comment

by:harsher
ID: 10651866
i need to generate at least 6 characters/numbers for the booking id and it must not be the same as others.... how can i do that?
0
 
LVL 6

Accepted Solution

by:
joghurt earned 400 total points
ID: 10653389
Well, for a unique ID you shouldn't rely on random (or pseudo-random) numbers because they might happen to be the same for two calls.
[... unless you store the random seed and re-init the pseudo-random generator next time.]

I don't know your eact environment for this booking program but simply incrementing the current booking ID might do, as well. Then you can get the maximal number already used when your program starts, and use the next number.

If you are too lazy to bother with storing any previous values or states, you can generate a GUID using the UuidCreate WinAPI function. It's 128-bits wide so it's virtually unique - even between different machines.
0
 

Author Comment

by:harsher
ID: 10659407
any codes to show me how can i do that? thanx
0
 
LVL 6

Expert Comment

by:joghurt
ID: 10661323
For which version?
My preferred solution is using GUIDs. ("Computers with ethernet/token ring addresses generate UUIDs that are guaranteed to be globally unique.")

#include <rpc.h>

UUID bookingID;
UuidCreate (&bookingID);

to display the UUID in some human-readable form:

unsigned char* str = NULL;
UuidToString (&bookingID, &str);
printf ("Booking ID is: %s\n", str);
RpcStringFree (&str);

[You may need to link Rpcrt4.lib to your project.]
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 10677811
If you store your booking ID's then I could give you a bit of code.  However as joghurt says what is wrong with the next in sequence?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

927 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