Archived

This topic is now archived and is closed to further replies.

Martyn

Easy Random Number

Recommended Posts

Martyn    122
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.

Share this post


Link to post
Share on other sites
vaneger    100
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);
}

Share this post


Link to post
Share on other sites
Enselic    829
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

Share this post


Link to post
Share on other sites
prh99    520
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 Explicit

Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As GUID) As Long

Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End 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 Function

error_olemsg:
MsgBox "
Error " & Str(Err) & ": " & Error$(Err)
GetGUID = "
00000000"
Exit Function

End Function


Function 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]

Share this post


Link to post
Share on other sites
twanvl    512
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]

Share this post


Link to post
Share on other sites
vaneger    100

//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);
*/

Share this post


Link to post
Share on other sites
psykr    295
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).

Share this post


Link to post
Share on other sites
aftermath    100
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]

Share this post


Link to post
Share on other sites
YoshiN    122
quote:
Original post by psykr
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).


Macros are really really bad unless you absolutely need them.

Share this post


Link to post
Share on other sites
Enselic    829
One reason out of many:
#define NUM_OF_ITEMS 12+15 
int not_what_you_meant_it_to_be = 4 * NUM_OF_ITEMS;


[edited by - Enselic on January 19, 2004 1:29:15 PM]

Share this post


Link to post
Share on other sites
vaneger    100
macros are pre compiled thus you would get the same "random numbers" for each compilation of the program but they would be static at run time, which is not what you want for random numbers.

i didnt take notice to the VB hints in his first post but i see them now.

my code is rather fast and it works well enough for games that dont need secure pseudo random numbers.

Share this post


Link to post
Share on other sites