Random Number of Long

D

Doctor

I need to generate a random long number. Each team in my software has been
assigned a random number which is used to assign teams to lanes for an event.

But then I must separate teams from the same group so that they are not
competing on adjacent lanes. My code loops through each team. If it finds
teams with the same group ID next to each other, it changes the random
number. To date I have not been able to get the random number to work right.
I either get a number with a decimal, the number 1, or always a positive
number, or always a negative number. So I implemented to code that you see
below for now.

I need my random number to be any number that a long can be.

I sure would appreciate your help. Thanks in advance


****Code****
If intLoopTest < 5 Then
If intNextTeamID > 0 Then
intNewTeamID = intNextTeamID - 434567890
Else
intNewTeamID = intNextTeamID + 434567890
End If
Else
If intNextTeamID > 0 Then 'A long can only be up to
2147,483,648
intNewTeamID = intNextTeamID - 1147483648
Else
intNewTeamID = intNextTeamID + 644245094#
End If
End If
 
T

Tom van Stiphout

On Wed, 25 Mar 2009 14:45:01 -0700, Doctor

This code does not show your use of the Rnd function so it will be
hard to help you with it. Other than to say that your current approach
isn't very random.
Did you read the Rnd function's help page? It has an example on how to
get a random number in an arbitrary number range.

-Tom.
Microsoft Access MVP
 
D

Doctor

Tom, thanks for your response. The intNextTeamID and the intNewTeamID are
both already random numbers. I have used the rnd() function for this but got
the results as previously mentioned: 1, or long decimal number, only postive
or only negative numbers.

What I am currently doing works for the most part to separate the teams, but
usually once out of twenty events, I will still end up with two teams from
the same group next to each other on the lanes.
 
J

John W. Vinson

Tom, thanks for your response. The intNextTeamID and the intNewTeamID are
both already random numbers. I have used the rnd() function for this but got
the results as previously mentioned: 1, or long decimal number, only postive
or only negative numbers.

Again:

Did you read Tom's message?
Did you read the Help file for Rnd()?

Rnd() returns a Double Float number greater than or equal to 0, and less than
1. That's what it's designed to do.

If you want to get an integer between 0 and 2147483647 multiply Rnd() by
2147483647 and truncate to an integer:

CLng(Rnd() * 2147483647)
 
D

Doctor

Thanks John,

Yes, I've read the help for rnd many times but have only received the
results mentioned.

About your suggestions: out of the 25 teams that were originally adjacent to
each other not one of them received a negative random number for a new ID.
I've run this many times and havn't received a negative number yet.

I would like for my number to be anywhere between -2147483647 and
2147483647. If this is not possible, is there a way for the random number
generator in a table to only produce positive numbers?

Thanks again.
 
J

John W. Vinson

I would like for my number to be anywhere between -2147483647 and
2147483647. If this is not possible, is there a way for the random number
generator in a table to only produce positive numbers?

Sorry... missed that part!

CLng(Rnd() * 4294967294. - 2147483647.)

Tried calling it six times in the Immediate window and got

?CLng(Rnd() * 4294967294. - 2147483647.)
-1952695039
897810944
1350724351
1119799295
-2087278335
1179999743
 
D

Doctor

Thanks so much. That took care of it. May I ask, How do you know all this
stuff? You've helped me many times for which I am very grateful! I don't
think that from looking at the help files or from any other posts that I
could find in forums that I would have ever figured that out on my own.

Is all of this information stored in your head or is there a reference book
that you would recommend for me.

Kudos for all your help both to me and to others. Doc.
 
J

John W. Vinson

Thanks so much. That took care of it. May I ask, How do you know all this
stuff? You've helped me many times for which I am very grateful! I don't
think that from looking at the help files or from any other posts that I
could find in forums that I would have ever figured that out on my own.

Is all of this information stored in your head or is there a reference book
that you would recommend for me.

Pretty much all in my head - and in knowing where to look for the help files.
I've been working with computers pretty steadily since 1967, and I've got a
good mind and a good memory. I've got lots of books but most of them are
sitting on the shelf across the office, gathering dust.
Kudos for all your help both to me and to others.

Very glad to have been of help... that's what keeps me going here, the
knowledge that all this bric-a-brac collected in my brain is actually doing
some good!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top