Solved

Random character/number generator in MFC?

Posted on 2004-03-22
8
918 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

627 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