genesiscircle
asked on
Rnd() values are changing everytime in ASP.NET
hi all,
IntTemp = Int((255 * Rnd()) + 1)
I used above ASP.NET code. Problem is in " Rnd() "
Rnd() value is changing everytime.
What is the alternative for Rnd()? OR How will stop Rnd() value changes at everytime?
Thanks
IntTemp = Int((255 * Rnd()) + 1)
I used above ASP.NET code. Problem is in " Rnd() "
Rnd() value is changing everytime.
What is the alternative for Rnd()? OR How will stop Rnd() value changes at everytime?
Thanks
change the 255 to a 0 and you'll get the same number each time.
rnd is a function for producing a random value
>> Rnd() values are changing everytime in ASP.NET
As gangwisch said, Rnd() gives a random number... Why would you expect a random number to stay the same?
ROFL@ bigphuckinglizard's answer =)
As gangwisch said, Rnd() gives a random number... Why would you expect a random number to stay the same?
ROFL@ bigphuckinglizard's answer =)
Rnd() is a function for producing a value, as OliWarner said.
Each time you call the Rnd() function you will get a different value.
However, you can get the numbers to come out in the same order each time by calling
Try this:
Randomize(25) 'whatever number here will affect which set of numbers will come up
for i=0 to 10
Response.Write rnd(100).ToString()+"<br>" 'print a number between 0-99
next
Now note I haven't actually tested this... but theoretically it should print 10 numbers to the page. They'll all (probably) be different. But the same 10 should come up each time.
Gosh, it's been a very long time since I've used VB ;)
Each time you call the Rnd() function you will get a different value.
However, you can get the numbers to come out in the same order each time by calling
Try this:
Randomize(25) 'whatever number here will affect which set of numbers will come up
for i=0 to 10
Response.Write rnd(100).ToString()+"<br>"
next
Now note I haven't actually tested this... but theoretically it should print 10 numbers to the page. They'll all (probably) be different. But the same 10 should come up each time.
Gosh, it's been a very long time since I've used VB ;)
Hmmmm, I've actually tested this version...
Randomize(25) 'whatever number here will affect which set of numbers will come up
Dim i As Integer
For i = 0 To 10
Response.Write(CInt(Rnd() * 100).ToString()+"<br>") 'print a number between 0-99
Next
If you actually wanted to round a number, use CInt(x)
Randomize(25) 'whatever number here will affect which set of numbers will come up
Dim i As Integer
For i = 0 To 10
Response.Write(CInt(Rnd() * 100).ToString()+"<br>") 'print a number between 0-99
Next
If you actually wanted to round a number, use CInt(x)
Interesting addition: this question made it to the Daily WTF. See also http://thedailywtf.com/forums/thread/103463.aspx
Doesn't seem that fair to me though. When testing an application, you might want to set a starting seed for the random numbers so you can predict which number will be next. Therefore I think Jivlain gave a correct answer to a perfectly valid question.
Doesn't seem that fair to me though. When testing an application, you might want to set a starting seed for the random numbers so you can predict which number will be next. Therefore I think Jivlain gave a correct answer to a perfectly valid question.
Try this function
Function UnRnd()
UnRnd = 4 'my favourite number!
End Function
IntTemp = Int((255 * UnRnd()) + 1)
Function UnRnd()
UnRnd = 4 'my favourite number!
End Function
IntTemp = Int((255 * UnRnd()) + 1)
Yeah 4 is a good number
Recommend simon_thwaites for the points =)
Recommend simon_thwaites for the points =)
I'd just suggest to delete this Q because the question is just too dumb. :-) Seems to me that whomever asked it wasn't even serious. (Else he needs another job.)
Very much agreed. This thread is just too silly.
Suggestion: Move it to the lounge for more silliness to ensue or delete it.
Suggestion: Move it to the lounge for more silliness to ensue or delete it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This thread made it onto CodeProject too.
http://www.codeproject.com/script/Forums/View.aspx?fid=1159&msg=2901248
http://www.codeproject.com/script/Forums/View.aspx?fid=1159&msg=2901248
Jivlain,
but 42 isn't my favourite number ;)
Try the AnswertoLifetheUniverseand Everything () function, but it does take 7.5 Million years to execute, damn Java... they should have used C or something...
but 42 isn't my favourite number ;)
Try the AnswertoLifetheUniverseand