Solved

Random character/number generator in MFC?

Posted on 2004-03-22
8
909 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 100 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 44

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

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
Expand data scrubbing tool 13 32
Creating an alias to a directory so I can change the directory 'contents' by changing the alias 5 85
Line meaning 9 81
Problem to event 3 76
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

21 Experts available now in Live!

Get 1:1 Help Now