Print Page | Close Window

Random number generator

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=8376
Printed Date: 01 April 2026 at 1:12am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Random number generator
Posted By: Nathan
Subject: Random number generator
Date Posted: 28 December 2003 at 10:26am

I am trying to create an asp random number generator which will provide a number between 1 and 16:

<% 
Dim maxlinks=16
Randomize Timer
linksCode = ((9275 * CInt((RND * 32000) + 1000)) mod maxlinks)+1
%>

The code is used to select random links to be inlcuded on each page of my web site. The includes part can be seen working here:
http://www.corfu-travel-guide.info/includes/random_links.asp - http://www.corfu-travel-guide.info/includes/random_links.asp

The problem is, it doesn't seem very random! Other programmers I have spoken to have also had similar problems in creating a random number with VB - can anyone help.

Thanks

Nathan

 

 

 




Replies:
Posted By: Paul Lush
Date Posted: 29 December 2003 at 4:12am

<%

randomize

response.write rnd(maxlinks) + 1

%>

maybe just a little too simple....



Posted By: Nathan
Date Posted: 04 January 2004 at 12:08pm

Thanks Paul.

 

I have tried working with the code:
<%
dim i
for i = 1 to 10
randomize
response.write rnd(16) + 1
response.write "<HR>"
next
%>

This can be see working here:

http://www.corfu-travel-guide.info/includes/rand.asp - http://www.corfu-travel-guide.info/includes/rand.asp

But the results are as follows ( I am after just a result of a random number between 1-16) Thanks for any help you can offer.:

1.115078


1.032219
1.811956
1.207165
1.021537
1.844
1.111033
1.309933
1.978812
1.972176

 

 



Posted By: psycotik
Date Posted: 05 January 2004 at 2:23am

It might be due to you using mod. that its less random.

if you do a random number from 0 to maxlinks you might have a better randomness.



Posted By: Nathan
Date Posted: 05 January 2004 at 3:53pm

I'm not using MOD in the above script. All I am after is a random number between 1 and 16. Seems so simple, but I cannot seen to write the script.

Must be turkey I ate at Christmas - or the beer - it's clouding my mind.

Nathan

 



Posted By: psycotik
Date Posted: 05 January 2004 at 10:35pm

Try this...

Randomize Timer
RandNumber = CLng(Rnd * (maxlinks - 1)) + 1



Posted By: Flamewave
Date Posted: 07 January 2004 at 10:42am
something like CInt(Rnd * 1000) Mod 16 should do the trick, then put in a check to see if the number is bigger then 16, if it is, set it to 16.

-------------
- Flamewave

They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.


Posted By: earchiee
Date Posted: 09 January 2004 at 12:04am
I need to create a 6 digit random number every time my page is loaded can any one help please.


Posted By: Nathan
Date Posted: 09 January 2004 at 12:42am

Flamewave.

That's it - thanks loads. I have just added 1 to the result and it is fine.

I owe you a beer. Anytime you are passing the taverna, just pop in.

Thanks to everyone for their help and postings.

Cheers

Nathan

http://www.agni.gr - www.agni.gr

 




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net