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
Solved

Random character/number generator in MFC?

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 94
mixString challenge 36 131
How to determine file rename from EventLog ? 2 54
Dell Alienware Graphics Amplifier Driver for non-Alienware systems 18 341
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: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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