Easy Random Number

Started by
13 comments, last by Martyn 20 years, 3 months ago
Ok, I have only been programming for around a month now. I made my first program but I have one thing to finish it off fully. I need txtOrderNo.Text to feature a unique random number on entering the form. If possible, I would like an easy guide how to this. I have searched on the site but I can''t get it to work. Thanks.
Advertisement
well this is what i use for my random numbers

//random.h#pragma once#include<time.h>#include<stdlib.h>#define RANDOMIZE srand((unsigned)time(0));template <class type>type __forceinline __fastcall Random(type min,type max){	return( (rand() % int((max - min + 1)) ) + min);}
I am using vb6, I don''t have a clue how to use that code you posted.
Basically, I would like a UNIQUE random number using time input.
If I recall correctly, Rnd() or Rand() or Random() will generate a random number between 0 and 64000. If you want a random number between 0 and 100, write An_Int_Variable = Rnd()%101
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
quote:Original post by Martyn
Ok, I have only been programming for around a month now. I made my first program but I have one thing to finish it off fully. I need txtOrderNo.Text to feature a unique random number on entering the form. If possible, I would like an easy guide how to this. I have searched on the site but I can't get it to work. Thanks.



You can use the CoCreateGUID() function in the API. I used it while attempting to write a Gnutella client (each Gnutella Packet requires it own unique ID).


Here is some example code:

Option ExplicitDeclare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As GUID) As LongType GUID    Data1 As Long    Data2 As Integer    Data3 As Integer    Data4(7) As ByteEnd Type      Function GetGUID() As String            Dim lResult As Long    Dim lguid As GUID    Dim MyguidString As String    Dim MyGuidString1 As String    Dim MyGuidString2 As String    Dim MyGuidString3 As String    Dim DataLen As Integer    Dim StringLen As Integer    Dim i%    Dim cnt As Integer    Dim dt As String    Dim tmp As String    Const S_OK = 0        tmp = ""            On Error GoTo error_olemsg        lResult = CoCreateGuid(lguid)        If lResult = S_OK Then                   MyGuidString1 = Hex$(lguid.Data1)           StringLen = Len(MyGuidString1)           DataLen = Len(lguid.Data1)           MyGuidString1 = LeadingZeros(2 * DataLen, StringLen) _              & MyGuidString1 'First 4 bytes (8 hex digits)           MyGuidString2 = Hex$(lguid.Data2)           StringLen = Len(MyGuidString2)           DataLen = Len(lguid.Data2)           MyGuidString2 = LeadingZeros(2 * DataLen, StringLen) _              & Trim$(MyGuidString2) 'Next 2 bytes (4 hex digits)           MyGuidString3 = Hex$(lguid.Data3)           StringLen = Len(MyGuidString3)           DataLen = Len(lguid.Data3)           MyGuidString3 = LeadingZeros(2 * DataLen, StringLen) _              & Trim$(MyGuidString3) 'Next 2 bytes (4 hex digits)           GetGUID = MyGuidString1 & MyGuidString2 & MyGuidString3           Dim sByte As String                           For i% = 0 To 7              sByte = Hex(lguid.Data4(i%))              If Len(sByte) < 2 Then sByte = "0" & sByte              MyguidString = MyguidString & sByte           Next i%           'MyGuidString contains last 8 bytes of Guid (16 hex digits)           GetGUID = GetGUID & MyguidString        Else           GetGUID = "00000000" ' return zeros if function unsuccessful        End If        'Convert to hex        For cnt = 1 To Len(GetGUID) Step 2            dt = Mid(GetGUID, cnt, 2)            tmp = tmp & Chr(Val("&h" & dt))        Next cnt        GetGUID = tmp                            Exit Functionerror_olemsg:         MsgBox "Error " & Str(Err) & ": " & Error$(Err)         GetGUID = "00000000"         Exit FunctionEnd FunctionFunction LeadingZeros(ExpectedLen As Integer, ActualLen As Integer) As String    LeadingZeros = String$(ExpectedLen - ActualLen, "0")End Function


in this example the output is in hex but that can very easily be changed. I am not too sure about the inner workings of the CoCreateGUID function but I believe it uses the system clock as part of process.


I hope this helps.



[edited by - prh99 on January 18, 2004 6:31:33 PM]

[edited by - prh99 on January 18, 2004 6:35:57 PM]
Patrick
In VB the Rnd() function will return a random number between 0 and 1. If you want the number to for example be between 1 and 10, use:
Private Sub Form_Load()    Randomize Timer ' This will initiate the random generator, if you don't use this line                     ' every run of the program will use the same sequence of random numbers    txtOrderNo.Text = "" & Int(Rnd() * 10 + 1)End Sub  


[Edit: Formatting was making the page too wide.]

[edited by - Oluseyi on January 18, 2004 7:06:12 PM]
//random.h#pragma once#include<time.h>#include<stdlib.h>#define RANDOMIZE srand((unsigned)time(0));template <class type>type __forceinline __fastcall Random(type min,type max){	return( (rand() % int((max - min + 1)) ) + min);}

//main.cpp#include<random.h>#include<iostream.h>int main(){ RANDOMIZE;//make sure the random numbers are random   cout<<Random(0,100)<<endl;//output a random number 0 to 100   cout<<Random(0.0, 2.0)<<endl;//output a random floating point   ///value 0.0 - 2.0}/* it just looks comfusing because it is setup to take any kind     of data (ints, floats, doubles, longs etc)   just call the function like this   Random(lowest number desired , highest number desired);*/
vaneger.. what''s wrong with you? Can''t you see he said "VB6"?
Besides, your code sucks. A simple macro would be superior to your code (if it compiled).
Function random(lowest As Double, highest As Double) As Double    random = lowest + Rnd * (highest - lowest)End Function


call random with the first arg as the lowest value that the random number can be, and the seconds arg as the highest value it can be.

[edited by - aftermath on January 18, 2004 8:26:13 PM]
Rate me up.

This topic is closed to new replies.

Advertisement