Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » random functions proposal
Re: random functions proposal [message #35139 is a reply to message #35137] Mon, 16 January 2012 18:31 Go to previous messageGo to previous message
Sender Ghost is currently offline  Sender Ghost
Messages: 301
Registered: November 2008
Senior Member
Hello, ratah, dolik.rce.

ratah wrote on Mon, 16 January 2012 17:06


I looked for a random function between an interval (a,b) and did not find.


dolik.rce wrote on Mon, 16 January 2012 17:58


you don't have to care about the range here as long as max < INT_MAX


There is another implementation:
#include <Core/Core.h>

using namespace Upp;

// Returns Random from a to b, inclusively.
dword Random(dword a, dword b)
{
	if (a == b)
		return a;
	if (a < b)
		return a + Random(b - a + 1);
	return b + Random(a - b + 1);
}

CONSOLE_APP_MAIN
{
	const int n = 100;
	for (int i = 1, j = n; i <= n; ++i, --j)
		Cout() << Format("%3d, %3d: %s\n", i, j, AsString(Random(i, j)));
}

[Updated on: Mon, 16 January 2012 18:48]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Init a ctrl inside INITBLOCK
Next Topic: problem with new and delete
Goto Forum:
  


Current Time: Sat May 04 17:04:15 CEST 2024

Total time taken to generate the page: 0.02893 seconds