Thursday, July 9, 2009

Generate some random unique Hex number.?

Hi,


i am just learning C and doing some coding for a project, I have to generate some unique number near about 1000 or 5000 which will be like that "00:16:2D:00:87:6E". it should be started from 00:16:2D:00:87:6E and end with 00:16:2D:00:97:0C. This "00:16:2D:00:" will be same for every number. These values will be for 3 number Num_1, Num_2 and Num_3


Num_1 - 17 Alpha-numeric char with :


Num_2 - Same as above +1


Num_3 - Same as above + 2





My question is there any easiest way to do that. I am not very good in C program. Can you please help me. Because i can't seem to figure out an easy way to generate random char arrays.





susikto

Generate some random unique Hex number.?
u can use any random no generation function like rand()


random()


and srand()


these are in turbo c++


may be there in c too


then take the mode of the random no. with 16


means


n=random_no % 16





this will return a no. from 0 to 15


then apply the condition


if n%26lt;10


take n-48 // since in ascii 0=48 and 1=49 and so on


// and u ll be assigning this to a char type variable


else


if n==10


take 'A'


else


if n==11


take 'B'


:


:


:


so on upto


if n==15


take 'F'





put this in a function





call this function again and agin to generate each character of the hexa decimal string


this way u can have four characters by calling it four times











u can append those characters in after those 6 alredy there in the no. string(that is a character array of size=no.of digits i.e. 12 here)








u can design random no. generator also urself using time(think upon it)


see in help of Turbo C





hope this will help u....





rate this as BEST ANSWER





hope this will help u
Reply:the formula to generate random numbers is (upperbound - lowerbound)*rnd+lowerbound.


No comments:

Post a Comment