473,320 Members | 2,020 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,320 software developers and data experts.

Random number problem

I am trying to create some random numbers to create an ID and am having
problems with getting the same number over and over.

I have a function:

Function RandomNumber(min as Integer, max as Integer) as integer
Dim random as Random = new Random()
RandomNumber = random.Next(min, max)
End Function

If I call this 3 times:

id = RandomNumber(0, 10))
id = RandomNumber(0, 10))
id = RandomNumber(0, 10))

I will get the same number each time.

I assume this is because it is based on the clock and uses the same seed if
call one after another. Is there a good way around this?

Thanks,

Tom
Feb 14 '06 #1
3 1496
> I am trying to create some random numbers to create an ID and am having
problems with getting the same number over and over.

Function RandomNumber(min as Integer, max as Integer) as integer
Dim random as Random = new Random()
RandomNumber = random.Next(min, max)
End Function


Replace Dim with Static:
Static random As New Random
What you want to do is call random.Next on the same object over and over
again. You don't want to make a new instance at every call.
Feb 14 '06 #2
"AMercer" <AM*****@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
I am trying to create some random numbers to create an ID and am having
problems with getting the same number over and over.

Function RandomNumber(min as Integer, max as Integer) as integer
Dim random as Random = new Random()
RandomNumber = random.Next(min, max)
End Function


Replace Dim with Static:
Static random As New Random
What you want to do is call random.Next on the same object over and over
again. You don't want to make a new instance at every call.


That was what I needed.

Thanks,

Tom
Feb 15 '06 #3
> I am trying to create some random numbers to create an ID and am
having problems with getting the same number over and over.

I assume this is because it is based on the clock and uses the same
seed if call one after another. Is there a good way around this?


In 2005, you may want to look into the Cryptography namespace, specifically
System.Security.Cryptography.RandomNumberGenerator . It is more reliable in
terms of creating true random results.

Jim Wooley
Feb 15 '06 #4

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

Similar topics

10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
4
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
40
by: RadiationX | last post by:
I have a problem that I really don't understand at all. In my previous post I could get started on my projects I just had a few problems with syntax errors. This problem is something that I don't...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
3
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
3
by: Manuel Ebert | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Dear list, who's got aesthetic advice for the following problem? I've got some joint probabilities of two distinct events Pr(X=x, Y=y), stored...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.