473,395 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How can I seed the Random object in VB with Now.Ticks?

Hi,

Here is C# code:

Random rdm1 = new

Random(unchecked((int)DateTime.Now.Ticks));

Can I do this in VB? I've tried CType(DateTime.Now.Ticks, Integer) and
CInt(DateTime.Now.Ticks) but both of them error out because the number is
too large. How can I do the cast like C# does? Thanks! Ken.
Nov 20 '05 #1
3 5382
Hi Ken,

C# is very kind. It says that if you have a Long and want it to become an
Int then enclose it in 'unchecked' and it'll chop off the bit that you don't
want.
IntValue = unchecked ((int) LongValue);

VB doesn't do this for us. We have to do it ourselves.

Dim LongValue As Long = &hFFFFFFFFFFFFFF
Dim IntValue As Integer = CInt (LongValue And &hFFFFFFFF&)

The '&h' at the front says that it's a number in hex while the '&' at the
end says that it's a Long value.

Regards,
Fergus
Nov 20 '05 #2
Hi everybody,

Here's a test:

1
Dim LongValue As Long = &hFFFFFFFFFFFFFF
Dim IntValue As Integer = CInt (LongValue And &hFFFFFFFF&)
2
Dim LongValue As Long = &hFFFFFFFFFFFFFF And &hFFFFFFFF&
Dim IntValue As Integer = CInt (LongValue)

What's the difference, if any, and if there is, can you explain why ?

Regards,
Fergus
Nov 20 '05 #3
"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in
news:OD**************@TK2MSFTNGP09.phx.gbl:

Just use the Environment.TickCount property:

Dim r As New Random(Environment.TickCount)


Can I do this in VB? I've tried CType(DateTime.Now.Ticks,

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Peter Ammon | last post by:
I'm calling random_shuffle without passing in a RandomNumberGenerator and getting the same shuffle every time I restart my program. Apparently I need to seed the internal random number generator. ...
4
by: vivek7006 | last post by:
By default, randomm module uses the timestamp to generate the seed value. Is it possible to know what that seed value is? import random random.random() # How do I print the current value of...
11
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using...
7
by: Glenn Wilson | last post by:
What would be the best way to generate random numbers for games. At the moment I use the following to seed the generator. Can anyone think of another way Random mRand = new...
10
by: Leon | last post by:
I know by default the random number generator use the time, but what is the best seed I can used in my web application? The Program generate 6 unique random numbers and load each of them in a...
4
by: ajikoe | last post by:
Hello, I tried calling RandomArray.seed() by calling RandomArray.get_seed() I get the seed number (x,y). My problem is that x is always 113611 any advice? Thanks pujo
3
by: clsmith66 | last post by:
I'm hoping someone can help me with a problem I'm having using the random class. I need to return a "random" number between 0 and 15, so i did this Dim r as new Random() Return r.Next(0, 15) ...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
8
by: Aspidus | last post by:
Hi, I wrote a function that generates a ulong I use it 3 times, one after the other, but it generates always the same numbers. At the end i get always 3 equal arrays. Any idea?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.