Solved

Random character/number generator in MFC?

Posted on 2004-03-22
8
908 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

706 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

19 Experts available now in Live!

Get 1:1 Help Now